![](/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.