Python | os.fsync() method
OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
os.fsync() method in Python is used to force write of the file associated with the given file descriptor.
In case, we are working with a file object( say f) rather than a file descriptor, then we need to use f.flush() and then os.fsync(f.fileno()) to ensure that all buffers associated with the file object f are written to disk.
Syntax: os.fsync(fd)
Parameter:
fd: A file descriptor for which buffer sync is required.
Return type: This method does not return any value.
Code #1: Use of os.fsync() method
Python3
import os
path = 'file.txt'
fd = os. open (path, os.O_RDWR)
str = b "GeeksforGeeks"
os.write(fd, str )
os.fsync(fd)
print ( "Force write of file committed successfully" )
os.close(fd)
|
Output:
Force write of file committed successfully
Code #2: If working with file objects
Python3
import os
path = 'file.txt'
f = open (path, 'w' )
str = "GeeksforGeeks"
f.write( str )
f.flush()
os.fsync(f.fileno())
print ( "Force write of file committed successfully" )
f.close()
|
Output:
Force write of file committed successfully
Last Updated :
12 Oct, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...