如何使用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
热门推荐
2 成都训练狗狗
3 怎么训练小狗狗
4 训练狗狗软件
5 如何训练狗狗拉屎
9 狗狗负重训练
10 狗狗多大可以训练大小便
11 怎么训练狗狗萨摩耶
12 狗狗训练书
13 鞍山狗狗训练
14 训练狗狗用品
15 怎样才能训练狗狗听话
16 狗狗如何训练大小便
17 怎样训练狗狗睡狗窝
18 训练狗狗工具