How come a file doesn't get written until I stop the program?

前端 未结 7 2074
旧时难觅i
旧时难觅i 2020-11-29 03:31

I\'m running a test, and found that the file doesn\'t actually get written until I control-C to abort the program. Can anyone explain why that would happen?

I expec

相关标签:
7条回答
  • 2020-11-29 04:13

    Writing to disk is slow, so many programs store up writes into large chunks which they write all-at-once. This is called buffering, and Python does it automatically when you open a file.

    When you write to the file, you're actually writing to a "buffer" in memory. When it fills up, Python will automatically write it to disk. You can tell it "write everything in the buffer to disk now" with

    f.flush()
    

    This isn't quite the whole story, because the operating system will probably buffer writes as well. You can tell it to write the buffer of the file with

    os.fsync(f.fileno())
    

    Finally, you can tell Python not to buffer a particular file with open(f, "w", 0) or only to keep a 1-line buffer with open(f,"w", 1). Naturally, this will slow down all operations on that file, because writes are slow.

    0 讨论(0)
提交回复
热议问题