os.fsync() Method
Description
The method fsync() forces write of file with file descriptor fd to disk. If you're starting with a Python file object f, first do f.flush(), and then do os.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.
Syntax
Following is the syntax for fsync() method −
os.fsync(fd)
Parameters
fd − This is the file descriptor for buffer sync is required.
Return Value
This method does not return any value.
Example
The following example shows the usage of fsync() 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"
b=line.encode()
os.write(fd, b)
# Now you can use fsync() method.
# Infact here you would not be able to see its effect.
os.fsync(fd)
# Now read this file from the beginning
os.lseek(fd, 0, 0)
line = os.read(fd, 100)
b=line.decode()
print ("Read String is : ", b)
# Close opened file
os.close( fd )
print ("Closed the file successfully!!")
When we run the above program, it produces the following result −
Read String is : this is test Closed the file successfully!!
python_os_file_directory_methods.htm
Advertisements