Do I need to os.fsync() before f.close()?
Asked Answered
I

3

8

"what exactly the python's file.flush() is doing?" says you should first f.flush() and then os.fsync(f.fileno()) to make sure the data are written to the disk.
Furthermore, "does close() imply flush() in Python?" claims that f.close() implies a flush().

Now, the question is: should I do

os.fsync(f.fileno())
f.close()

or, does f.close() also implies an os.fsync()?

Here are the doc of Python IO, the doc of Python file close, and the source code. A quick search of 'fsync' returned no relevant info.

Impermanent answered 16/10, 2019 at 1:21 Comment(3)
probably all people use only f.close() and nobody had problem with saved data.Henandchickens
@Henandchickens That is true. But can I be sure that my data have finished saving to drive when close() returns?Impermanent
@DanielChin note that fsync take a diff argument. os.fsync(f.fileno())Masonmasonic
M
5

I checked myself by stracing a test script and no it does not imply an fsync. f.close() implies a f.flush() because it has to. f.flush() writes the internal (user space) buffers to the associated file descriptor and you can not write to a file descriptor after closing it. However writing to the kernel buffers does not guarantee durability and this is where fsync comes into play.

Motherwort answered 24/10, 2021 at 20:8 Comment(3)
Thank you very much. Therefore it looks like I should do fsync before displaying "you may now turn the power off", right?Impermanent
@DanielChin Shutting down your system will most likely also make all kernel I/O buffering durable, but to be on the safe side you probably should enforce fsync under the assumption there are no major performance concerns. :)Capsular
If you are going to shut it down you can instead use os.sync() which takes care of all the write buffers.Conchiolin
G
0

I have tried both os.fsync() then f.close() and os.fsync() only. Both will be giving the same result of immediate data written to the file. The file is updated using either methods despite a sudden power off occur at the main electrical outlet and power back on later.

So, f.close() is not necessary when there's os.fsync().

Gruelling answered 11/3, 2021 at 10:54 Comment(1)
Thank you for your answer. Officially though, is f.close documented to return after writing to the disk?Impermanent
A
0

Also beware that every OS might have different effects. For example, on Windows you can make sure that a file rename gets flushed to disk by opening the file and doing a fsync on it after having renamed it (it flushes metadata changes to disk).

Asir answered 11/9 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.