os.fsync() 方法



描述

fsync() 方法强制将文件描述符 fd 对应的文件写入磁盘。如果从 Python 文件对象 f 开始,首先执行 f.flush(),然后执行 os.fsync(f.fileno()),以确保与 f 相关的所有内部缓冲区都写入磁盘。

语法

以下是 fsync() 方法的语法:-

os.fsync(fd)

参数

  • fd − 这是需要缓冲区同步的文件描述符。

返回值

此方法不返回值。

示例

以下示例显示了 fsync() 方法的使用:-

import os, sys
# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# Write one string
line="this is test"
b=line.encode()
os.write(fd, b)

# Now you can use fsync() method.
# Infact here you would not be able to see its effect.
os.fsync(fd)

# Now read this file from the beginning
os.lseek(fd, 0, 0)
line = os.read(fd, 100)
b=line.decode()
print ("Read String is : ", b)

# Close opened file
os.close( fd )
print ("Closed the file successfully!!")

运行以上程序时,会产生以下结果:-

Read String is : this is test
Closed the file successfully!!
python_os_file_directory_methods.htm
广告

© . All rights reserved.