Python os.fsync() Method

Python File(文件) 方法Python 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 manipulate a Python file object f, first f.flush(), then os.fsync(f.fileno()), to ensure that all memory associated with f has been written to the hard disk. Effective on Unix and Windows.

Available on Unix and Windows.

Syntax

fsync()The method syntax is as follows:

os.fsync(fd)

Parameters

  • fd-- file descriptor.

Return Value

This method has no return value.

Example

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-

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 execution is:

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

Python File(文件) 方法Python OS File/Directory Methods

Other Extensions