如果
with open("xxxx.txt", mode="w", encoding="utf-8") as f:
f.write("")
这样的话 会清空吗?
还有没有其他的 直接清空的指令
注意 不能删除文件
如果
with open("xxxx.txt", mode="w", encoding="utf-8") as f:
f.write("")
这样的话 会清空吗?
还有没有其他的 直接清空的指令
注意 不能删除文件
会清空的。w模式是write,会覆写文件的所有内容,a模式才是添加。
也可以用truncate方法:
with open("xxxx.txt", mode="w", encoding="utf-8") as f:
f.truncate(0)