我正在嘗試從 for 循環輸出文本。這是我目前的實現with open('timelog.txt', 'w') as time_log: for x in range(100): time_log.write(t'/ntest')time_log.close()我目前什么也沒寫。文本文件已創建,但似乎它實際上根本沒有被修改。有什么我想念的嗎?
1 回答

慕尼黑8549860
TA貢獻1818條經驗 獲得超11個贊
不要使用,time_log.close()因為with上下文管理器會自動關閉文件。
with open("file.txt", "w") as ff:
for i in range(10):
ff.write("%d\n" % i)
然后檢查文件:
$ cat file.txt
0
1
2
3
4
5
6
7
8
9
添加回答
舉報
0/150
提交
取消