Python os.fdatasync()方法
Python中的os.fdatasync()方法用于强制写入与给定文件描述符相关的文件。与os.fsync()方法不同,它不强制更新元数据。
os.fdatasync()方法比os.fsync()方法更快,因为它只需要强制写入一个磁盘而不是两个。
os.fdatasync 语法
os.fdatasync(fd)
os.fdatasync 参数
fd:要为其写入数据的文件描述符。
返回类型:此方法不返回任何值。
os.fdatasync 示例1
使用os.fdatasync()方法
# Python program to explain os.fdatasync() method
# importing os module
import os
# File path
path = 'file.txt'
# Open the file and get
# the file descriptor
# associated with
# using os.open() method
fd = os.open(path, os.O_RDWR)
# Write a bytestring
str = b"GeeksforGeeks"
os.write(fd, str)
# The written string is
# available in program buffer
# but it might not actually
# written to disk until
# program is closed or
# file descriptor is closed.
# sync. all internal buffers
# associated with the file descriptor
# with disk (force write of file)
# using os.fdatasync() method
os.fdatasync(fd)
print("Force write of file committed successfully")
# Close the file descriptor
os.close(fd)
# os.fdatasync() method
# does not force update of
# metadata. if you want to
# update it too, use
# os.fsync() method instead.
输出:
Force write of file committed successfully