Python方法fdatasync()强制将具有filedescriptorfd的文件写入磁盘。这不强制更新元数据。如果你想刷新你的缓冲区,那么你可以使用这个方法。
os.fdatasync(fd) - 语法
os.fdatasync(fd);
fd - 这是要为其写入数据的文件描述符。
os.fdatasync(fd) - 示例
以下示例显示了fdatasync()方法-的用法
#!/usr/bin/python import os, sys # Open a file fd=os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) # Write one string os.write(fd, "This is test") # Now you can use fdatasync() method. # Infact here you would not be able to see its effect. os.fdatasync(fd) # Now read this file from the beginning. os.lseek(fd, 0, 0) str=os.read(fd, 100) print "Read String is : ", str # Close opened file os.close( fd ) print "Closed the file successfully!!"
当无涯教程运行上面的程序时,它产生以下输出-
Read String is : This is test Closed the file successfully!!