📜  Python| os.sync() 方法

📅  最后修改于: 2022-05-13 01:55:00.769000             🧑  作者: Mango

Python| os.sync() 方法

Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。
Python中的os.sync()方法用于强制将所有内容写入磁盘。此方法允许进程将所有脏缓冲区刷新到磁盘。
os.sync() os.fsync(fd)os.fdatasync(fd)方法之间的区别 –
os.sync()方法强制将所有内容写入磁盘,其中os.fsync(fd)方法强制写入与指定文件描述符 fd 关联的文件, os.fdatasync(fd)方法类似于os.fsync()方法,但它不会强制更新文件的元数据。
注意:此方法仅适用于 Unix 平台。

代码:使用os.sync()方法

Python3
# Python program to explain os.sync() method
   
# importing os module
import os
 
 
# File path 1
path1 = 'file.txt'
 
# File path 2
path2 = 'file2.txt'
 
# File path 3
path3 = 'file3.txt'
 
# Open the files and get
# the file descriptors
# associated with them
# using os.open() method
fd1 = os.open(path1, os.O_RDWR)
fd2 = os.open(path2, os.O_RDWR)
fd3 = os.open(path3, os.O_RDWR)
 
 
# Write a bytestring
str = b"GeeksforGeeks"
os.write(fd1, str)
os.write(fd2, str)
os.write(fd3, str)
 
 
# Sync. all buffers to disk
# i.e force write everything
# to disk using os.sync() method
os.sync()
print("Force write everything committed successfully")
 
# Close the file descriptors
os.close(fd1)
os.close(fd2)
os.close(fd3)
 
 
# os.sync() method
# will flush all buffers
# to disk.
# it may take a significant
# length of time


输出:
Force write of everything committed successfully

参考: https://docs。 Python.org/3/library/os.html#os.sync