YouTip LogoYouTip

Os Fsync

# Python2.x Python os.fsync() Method [![Image 3: Python File Method](#) Python OS File/Directory Methods](#) * * * ### Overview The os.fsync() method forces the file with file descriptor fd to be written to disk. On Unix, the fsync() function is called; on Windows, the _commit() function is called. If you are preparing to operate on a Python file object f, first call f.flush(), then os.fsync(f.fileno()), to ensure that all memory associated with f is written to disk. Effective on Unix and Windows. Available on Unix and Windows. ### Syntax The **fsync()** method syntax format is as follows: os.fsync(fd) ### Parameters * **fd** -- The file descriptor. ### Return Value This method has no return value. ### Example The following example demonstrates the use of the fsync() method: #!/usr/bin/python# -*- coding: UTF-8 -*-import os, sys # Open file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )# Write 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 "Read string is : ", str # Close file os.close( fd)print "Close file success!!" Execute the above program, the output result is: Read string is : This is test Close file success!! [![Image 4: Python File Method](#) Python OS File/Directory Methods](#)
← Os IsattyOs Fdopen β†’