English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Fonctions en Python

Types de données en Python

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python file flush() usage and example

Python File (File) Methods

Overview

flush() The method is used to refresh the buffer, that is, the data in the buffer is immediately written to the file, and the buffer is cleared at the same time, without waiting passively for the output buffer to be written.

Generally, the buffer area is automatically refreshed after the file is closed, but sometimes you need to refresh it before closing it, and in this case, you can use the flush() method.

Syntax

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

fileObject.flush();

Parameter

  • None

Return value

This method does not return a value.

Example

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

# Open the file
fo = open("w",3codebox.txt", "wb")
print("File name: ", fo.name)
 
# Flush the buffer
fo.flush()
 
# Close the file
fo.close()

The output result of the above example is as follows:

File name:  w3codebox.txt

Python File (File) Methods