Easy Tutorial
❮ Python Random Python Func Number Round ❯

Python3 File flush() Method

Python3 File Methods


Overview

The flush() method is used to flush the buffer, which means it immediately writes the buffered data to the file and clears the buffer. It does not require passive waiting for the output buffer to be written.

Normally, the buffer is automatically flushed when the file is closed. However, sometimes you need to flush it before closing, and that's when you can use the flush() method.

Syntax

The syntax for the flush() method is as follows:

fileObject.flush();

Parameters

-

None

Return Value

This method does not return any value.

Example

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

#!/usr/bin/python3

# Open file
fo = open("tutorialpro.txt", "wb")
print("File name: ", fo.name)

# Flush buffer
fo.flush()

# Close file
fo.close()

The output of the above example is:

File name:  tutorialpro.txt

Python3 File Methods

❮ Python Random Python Func Number Round ❯