![](/img/trans.png)
[英]Write all lines for each set of a range to new file each time the range changes Python 3.6
[英]Python won't write each time new lines into text file
我有两个python文件,它们都在同一个文件夹中。 主文件执行整个功能,使程序成为我想要的功能。 另一个文件将数据写入文本文件。 但是,将数据写入文本文件存在一个问题:与其每次都在现有文本中添加新行,不如将新行完全写入整个文件。
import codecs
def start(text):
codecs.open('D:\\Program Files (x86)\\Python342\\passguess.txt', 'a', 'utf-8')
with open('D:\\Program Files (x86)\\Python342\\passguess.txt', 'w') as file:
file.write(text + '\n')
我已经尝试了一些方法,例如.join(text)或从主文件中的writefile.py运行代码。 似乎没有任何效果。
问题在于线
with open('D:\\Program Files (x86)\\Python342\\passguess.txt', 'w') as file:
这将以写入模式打开文件,以追加您想要的“ a”选项,因此只需更改为
with open('D:\\Program Files (x86)\\Python342\\passguess.txt', 'a') as file:
你应该没事的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.