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

Python File flush() method

Python File flush() method

Python File methods Python File methods


Overview

flush() The method flushes the buffer, i.e. writes the buffered data to the file immediately and clears the buffer, without passively waiting for the output buffer to be written.

Normally, closing a file flushes the buffer automatically, but sometimes you need to flush it before closing, in which case you can use flush().

Syntax

The flush() method syntax is as follows:


fileObject.flush();

Parameters

Return Value

This method has no return value.

Example

The following example shows how to use the flush() method:

Example

#!/usr/bin/python

-- coding: UTF-8 --

Opening a File

fo

=

open

(

"

runoob.txt

"

,

"

wb

"

)

print

"

File name:

"

,

fo

.

name

Flush the buffer

fo

.

flush

(

)

Closing a File

fo

.

close

(

)

The output of the above example is:


文件名为:  runoob.txt

Python File methods Python File methods