Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python os.fsync() method

Python os.fsync() method

Python File methods Python OS file/directory methods


Overview

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

If you are working with a Python file object f, first call f.flush() and then os.fsync(f.fileno()) to make sure all memory associated with f is written to disk. Valid on Unix and Windows.

Available on Unix and Windows.

Syntax

The **fsync()** method syntax is as follows:



os.fsync(fd)

Parameters

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



# Open file

fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )



# Write string

os.write(fd, "This is test")



# Use fsync() method.

os.fsync(fd)



# Read content

os.lseek(fd, 0, 0)

str = os.read(fd, 100)

print "读取的字符串为 : ", str



# Close file

os.close( fd)



print "关闭文件成功!!"

The output of the above program is:



读取的字符串为 :  This is test

关闭文件成功!!

Python File methods Python OS file/directory methods