看板 Python 關於我們 聯絡資訊
Hi, 各位好!想請教一個寫檔的問題。 我發現python寫檔好像是一次性把全部寫入。例如: #!/usr/bin/env python import time of = open("output.dat", "w") for i in range(0, 100): time.sleep(1) of.write("test\n") of.close() 我用tail -f output.dat觀察有無文字出現, 感覺是要等到of.close()才會一次寫入。 請問有方法可以同部寫入嗎? 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.184.14
carlcarl:在 of.write("test\n") 之後 of.flush() 11/05 03:26
uranusjr:這行為所有語言都一樣... 11/05 08:45
adair326:謝謝! 11/05 11:10
gp123:為了顧及效率,寫檔通常會經過buffer(緩衝), 11/06 15:50
gp123:待適當時機(如檔案關閉)或明確指定要flush才會一次寫入 11/06 15:51
gp123:相關內容請見檔案處理的相關文件 11/06 15:51