Easy Tutorial
❮ Python Att Dictionary Items Python Func Number Log10 ❯

Python3 os.fsync() Method

Python3 OS File/Directory Methods


Overview

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

If you intend to operate on a Python file object f, first use f.flush(), then os.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk. This is effective on Unix and Windows.

Available on Unix, Windows.

Syntax

fsync() method syntax is as follows:

os.fsync(fd)

Parameters

-

fd -- The file descriptor of the file.

Return Value

This method does not return any value.

Example

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

#!/usr/bin/python3

import os, sys

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

# Write a string
os.write(fd, "This is test")

# Use fsync() method.
os.fsync(fd)

# Read the content
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print("Read string is : ", str)

# Close the file
os.close(fd)

print("Successfully closed the file!!")

Executing the above program outputs:

Read string is :  This is test
Successfully closed the file!!

Python3 OS File/Directory Methods

❮ Python Att Dictionary Items Python Func Number Log10 ❯