Python方法fsync()强制将具有文件描述符fd的文件写入磁盘。如果您从Python文件对象f开始,首先执行f.flush(),然后执行os.fsync(f.fileno(),以确保与f关联的所有内部缓冲区都写入磁盘。

os.fsync(fd) - 语法

os.fsync(fd)
  • fd    -  这是缓冲区同步所需的文件描述符。

os.fsync(fd) - 示例

以下示例显示了fsync()方法的用法。

#!/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 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)
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!!

参考链接

https://www.learnfk.com/python/os-fsync.html