Python3 os.fsync() Method

Python3 OS 文件/目录方法Python3 OS File/Directory Methods


Overview

The os.fsync() method forces the file with file descriptor fd to be written to the hard disk. On Unix, the fsync() function is called; on Windows, the _commit() function is called.

If you are going to operate on a Python file object f, first call f.flush(), then os.fsync(f.fileno()), to ensure that all memory related to f is written to the hard disk. This is effective on Unix and Windows.

Available on Unix and Windows.

Syntax

fsync()The method syntax is as follows:

os.fsync(fd)

Parameters

  • fd-- The file descriptor.

Return Value

This method has no return value.

Example

The following example demonstrates the use of the fsync() method:

#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 写入字符串
os.write(fd, "This is test")

# 使用 fsync() 方法.
os.fsync(fd)

# 读取内容
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print ("读取的字符串为 : ", str)

# 关闭文件
os.close( fd)

print ("关闭文件成功!!")

The output of the above program is:

读取的字符串为 :  This is test
关闭文件成功!!

Python3 OS 文件/目录方法Python3 OS File/Directory Methods

Other Extensions