Python os.fdatasync() Method
os.fdatasync() Overview
The os.fdatasync() method is used to force a file to be written to disk, which is specified by the file descriptor fd, but does not force the status information of the file to be updated. You can use this method if you need to flush the buffer.
Available on Unix.
os.fdatasync() Syntax
The fdatasync() method syntax format is as follows
os.fdatasync(fd);
os.fdatasync() Parameters
- fd – file descriptor
os.fdatasync() Return Value
This method has no return value.
os.fdatasync() Demo
The following example demonstrates the use of the fdatasync() method:
#!/usr/bin/python3
import os, sys
# Open the file "/tmp/foo.txt"
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Write string
os.write(fd, "This is test")
# Using the fdatasync() method
os.fdatasync(fd)
# Reading files
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print ("The characters to be read are : ", str)
# Close file
os.close( fd )
print ("Close file successfully!!!")
The output of executing the above program is:
The characters to be read are : This is test
Close file successfully!!!