擅长:python、mysql、java
<p>我建议逐行将文件写入一个新文件,然后将新文件移动到原始文件,这样会覆盖它:</p>
<pre><code>import shutil
filename = '2017-11-02.txt'
temp_filename = 'new.txt'
with open(filename, 'r') as old, open(temp_filename, 'w') as new:
# Go line by line in the old file
for line in old:
# Write to the new file
new.write('%s %s\n' % (line.strip(),'checked'))
shutil.move(temp_filename, filename)
</code></pre>
<p>有点相关的答案,我基于:<a href="https://stackoverflow.com/a/16604958/5971137">https://stackoverflow.com/a/16604958/5971137</a></p>