代码示例:(标识:demo_python_ref_file_truncate)
demo_python_ref_file_truncate.py
f = open("demofile2.txt", "a")
f.truncate(20)
f.close()

#open and read the file after the truncate:
f = open("demofile2.txt", "r")
print(f.read())
运行结果: