如何使用Python强制将具有filedescriptor fd的文件写入磁盘?
您必须使用fdatasync(fd)函数强制将文件描述符为fd的文件写入磁盘。它不强制更新元数据。另请注意,这仅在Unix上可用。
跨平台的另一种解决方案是使用fsync(fd),因为它会强制将文件描述符为fd的文件写入磁盘。在Unix上,这将调用本机fsync()函数。在Windows上,MS_commit()函数。
示例
import os, sys # Open a file fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT ) os.write(fd, "This is test") # Now you can use fsync() method. 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 os.close( fd )
输出结果
当我们运行上面的程序时,它产生以下结果:
Read String is : This is test
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短