YouTip LogoYouTip

Python3 Os Fsync

# Python3.x Python3 os.fsync() Method [![Image 3: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#) * * * ### Overview The os.fsync() method forces the file with file descriptor fd to be written to disk. On Unix, it will call the fsync() function; on Windows, it will call the _commit() function. If you are working with a Python file object f, first call f.flush(), then os.fsync(f.fileno()), to ensure all memory associated with f is written to disk. It works on Unix and Windows. Available on Unix and Windows. ### Syntax The syntax for the **fsync()** method is as follows: os.fsync(fd) ### Parameters * **fd** -- The file descriptor. ### Return Value This method does not return a value. ### Example The following example demonstrates the use of the fsync() method: #!/usr/bin/python3import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )# Write a string os.write(fd, "This is test")# Use fsync() method. os.fsync(fd)# Read content os.lseek(fd, 0, 0) str = os.read(fd, 100)print ("The read string is : ", str)# Close the file os.close( fd)print ("Closed the file successfully!!") The output of executing the above program is: The read string is : This is test Closed the file successfully!! [![Image 4: Python3 OS File/Directory Methods](#) Python3 OS File/Directory Methods](#)
← Python3 Os GetcwdPython3 Os Fsync β†’