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

标签 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()

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

最佳答案

使用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().

关于python - 如何在instance.save()之后删除input.txt文件的所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16820485/

相关文章:

python - 南迁: "database backend does not accept 0 as a value for AutoField" (mysql)

python - 找到样本数量不一致的输入变量: [rows, columns]

python - 如何在 Go 中将文本复制到剪贴板或从剪贴板复制文本?

python - 对 matplotlib 颜色的颜色代码感到好奇

python - 从文本文件中读取 np 数组

python - Mysql 或 python 中的数据类型转换

python - 使用 BeautifulSoup 获取 li 中的文本

获得两个列表之间初始差异的 Pythonic 方法

python - 检查 RDD 中是否存在值

python - 从 Coinbase Exchange API Websocket 接收数据