Python File flush() Method

Python File(文件) 方法Python File Methods


Overview

flush()The method is used to flush the buffer, that is, to immediately write the data in the buffer to the file and clear the buffer, without passively waiting for the output buffer to be written.

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

Syntax

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

fileObject.flush();

Parameters

  • None

Return Value

This method has no return value.

Example

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

Example

#!/usr/bin/python # -*- coding: UTF-8 -*- # Open the file fo = open("example.txt", "wb") print "The file name is:", fo.name # Flush the buffer fo.flush() # Close the file fo.close()

The output of the above example is:

文件名为:  example.txt

Python File(文件) 方法Python File Methods

Other Extensions