Reputation: 3767
I'm writing to a file and I call flush() after I'm done. Is it safe to force quit the program say pulling the power plug without calling close() on the file? Is calling flush() sufficient that the file won't become corrupt.
Upvotes: 1
Views: 78
Reputation: 295649
To ensure that content is flushed to the operating system (and is visible to other running applications), either flush()
or close()
will suffice (which is to say, you don't need both).
To ensure that content is flushed to disk, you also need to add os.fsync()
or os.fdatasync()
. Do note that in the case of a newly-created (or newly-renamed) file, you need to worry about whether the directory was flushed as well.
By the way -- if you care about being really sure things get to disk, the classic presentation Eat My Data is worth your time and attention.
Upvotes: 3