如何在instance.save()之后删除input.txt文件的所有内容?

时间:2013-05-29 17:38:24

标签: python

text = "sample sample"
text_file = open("input.txt", "r+b")
text_file.write(text)
instance.src_after = File(text_file)
instance.save()
text_file.close()

如何在input.txt之后删除instance.save()文件的所有内容?

1 个答案:

答案 0 :(得分:2)

使用file.truncate

text_file.truncate(0)

file.truncate上的帮助:

>>> print file.truncate.__doc__
truncate([size]) -> None.  Truncate the file to at most size bytes.

Size defaults to the current file position, as returned by tell().