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 に答える