Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想从我的data.txt文件中删除所有数据:
我怎样才能做到这一点?
with open('data.txt', 'w'): pass
这将删除 的内容'data.txt',留下一个空文件。(这也适用于 Python3)。
'data.txt'
做就是了:open(file,'w').close()
open(file,'w').close()
open(file, 'w')可以为你工作。不要忘记关闭文件或使用with.
open(file, 'w')
with