[英]How to delete all characters after a “==” in each line of a file and update the file, using python script?
[英]How to delete all characters in text file by python
我有一個文本文件namefile.txt
,其中包含更多字符。 我想刪除所有文本並保留文件,以便在運行python腳本后打開namefile.txt
時看不到任何內容?
這樣就可以了。 從概念上講,打開具有寫許可權的文件會清除以前的數據。
open('namefile.txt', 'w').close()
只需在寫入模式下打開文件並關閉它
f = open('myfile.dat', 'w')
f.close()
如果要保持文件打開,也可以調用:file.truncate()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.