如何使用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
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬