[英]How to delete all characters in text file by python
I have a text file namefile.txt
which has more character. 我有一个文本文件namefile.txt
,其中包含更多字符。 I want to delete all text and keep the file in order to see nothings when we open namefile.txt
after run the python script? 我想删除所有文本并保留文件,以便在运行python脚本后打开namefile.txt
时看不到任何内容?
This will do it. 这样就可以了。 Opening a file with write permission conceptually clears previous data. 从概念上讲,打开具有写许可权的文件会清除以前的数据。
open('namefile.txt', 'w').close()
Simply open the file in write mode and close it 只需在写入模式下打开文件并关闭它
f = open('myfile.dat', 'w')
f.close()
如果要保持文件打开,也可以调用:file.truncate()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.