Latest web development tutorials

Python3 os.fsync () method

Python3 OS file / directory methods Python3 OS file / directory methods


Outline

os.fsync () method to force the file descriptor fd of the file is written to the hard disk. In Unix, will call fsync () function; on Windows, call _commit () function.

If you are ready to operate a Python file object f, first f.flush (), then os.fsync (f.fileno ()), to ensure that all memory associated with f are written to the hard disk. Valid unix, Windows in.

Unix, is available on Windows.

grammar

fsync () method syntax is as follows:

os.fsync(fd)

parameter

  • fd - the file descriptor.

return value

This method has no return value.

Examples

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

#!/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 above program output is:

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

Python3 OS file / directory methods Python3 OS file / directory methods