1
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()

4

1 回答 1

1

使用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().
于 2013-05-29T17:39:32.050 回答