Python| os.fsync() 方法
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.fsync()方法用于强制写入与给定文件描述符关联的文件。
如果我们使用的是文件对象(比如 f)而不是文件描述符,那么我们需要使用f.flush()和 os.fsync(f.fileno()) 来确保与文件对象 f 被写入磁盘。
Syntax: os.fsync(fd)
Parameter:
fd: A file descriptor for which buffer sync is required.
Return type: This method does not return any value.
代码 #1:使用os.fsync()方法
Python3
# Python program to explain os.fsync() 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.fsync() method
os.fsync(fd)
print("Force write of file committed successfully")
# Close the file descriptor
os.close(fd)
Python3
# Python program to explain os.fsync() method
# importing os module
import os
# File path
path = 'file.txt'
# Open the file and get
# the file object
# using open() method
f = open(path, 'w')
# Write a string to
# the file object
str = "GeeksforGeeks"
f.write(str)
# Firstly, flush internal buffers
f.flush()
# Now, sync. all internal buffers
# associated with the file object
# with disk (force write of file)
# using os.fsync() method
os.fsync(f.fileno())
print("Force write of file committed successfully")
# Close the file object
f.close()
输出:
Force write of file committed successfully
代码 #2:如果使用文件对象
Python3
# Python program to explain os.fsync() method
# importing os module
import os
# File path
path = 'file.txt'
# Open the file and get
# the file object
# using open() method
f = open(path, 'w')
# Write a string to
# the file object
str = "GeeksforGeeks"
f.write(str)
# Firstly, flush internal buffers
f.flush()
# Now, sync. all internal buffers
# associated with the file object
# with disk (force write of file)
# using os.fsync() method
os.fsync(f.fileno())
print("Force write of file committed successfully")
# Close the file object
f.close()
输出:
Force write of file committed successfully