Python OS file/directory os.fdatasync() method
Python OS File/Directory os.fdatasync() Method
Description
The fdatasync() method forces the file with file descriptor fd to be written to disk. This does not force an update of the metadata. If you want to flush the buffer, you can use this method.
Syntax
Following is the syntax of the fdatasync() method –
os.fdatasync(fd)
Parameters
- fd − This is the file descriptor to which the data is to be written.
Return Value
This method does not return any value.
Example
The following example shows the usage of the fdatasync() method−
import os, sys
#Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Write one string
line="this is test"
# string needs to be converted byte object
b=str.encode(line)
os.write(fd, b)
# Now you can use fdatasync() method.
# Infact here you would not be able to see its effect.
os.fdatasync(fd)
# Now read this file from the beginning.
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
line = os.read(fd, 100)
str=line.decode()
print ("Read String is: ", str)
# Close the opened file
os.close( fd )
print ("Closed the file successfully!!")
When we run the above program, it will produce the following output –
Read String is: This is test
Closed the file successfully!!