Python的os.fdatasync()
方法强制将与fd
相关联的文件写入到磁盘。这不会强制更新元数据。如果要刷新缓冲区,则可以使用此方法。
语法
以下是fdatasync()
方法的语法 -
os.fdatasync(fd, uid, gid)
参数
- fd − 这是要写入数据的文件描述符。
返回值
- 此方法不返回任何值。
示例
以下示例显示了fdatasync()
方法的用法。
#!/usr/bin/python3
import os, sys
# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Write one string
line = "this is test"
# string needs to be converted byte object
b = str.encode(line)
os.write(fd, b)
# 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)
line = os.read(fd2, 100)
str = line.decode()
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!!
上一篇:
Python os模块方法
下一篇:
Python异常处理