+

Writing a file from filedescriptor fd to disk using Python?

You must use the fdatasync (fd) function to force the file with file descriptor fd to be written to disk. This does not force the metadata to be updated. Also note that this is only available on Unix. 

A more cross-platform solution would be to use fsync (fd), since it causes the file with file descriptor fd to be written to disk. On Unix, this calls the built-in fsync () function; on Windows the MS _commit () function.



Example

 import os, sys # Open a file fd = os.open (" foo.txt ", os.O_RDWR | os. O_CREAT) os.write (fd," This is test " ) # Now you can use fsync () method. os.fsync (fd) # Now read this file from the beginning os.lseek (fd, 0, 0) str = os.read (fd, 100) print "Read String is:", str os.close (fd) < / pre> 

Output

When we run the above program, it produces the following output:



 Read String is: This is test 



Get Solution for free from DataCamp guru