Python| os.fdatasync() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.fdatasync()
方法用于强制写入与给定文件描述符关联的文件。与os.fsync()
方法不同,它不强制更新元数据。
os.fdatasync()
方法比os.fsync()
方法快,因为它只需要强制一个磁盘写入而不是两个。
Syntax: os.fdatasync(fd)
Parameter:
fd: A file descriptor for which data is to be written.
Return type: This method does not return any value.
代码: 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
参考: https://docs。 Python.org/3/library/os.html#os.fdatasync