Latest web development tutorials

Python File (File) method

file functions to create objects using open, the following table lists the file objects commonly used functions:

No. Method and Description

file.close ()

Close the file. Close the file can no longer read and write operations.


file.flush ()

Internal refresh file buffering, the data directly to the internal buffer written to the file immediately, rather than passively waiting for output buffer written.


file.fileno ()

Return an integer file descriptor (file descriptor FD integer), it can be used in such a method os module read some of the underlying operations.


file.isatty ()

If the file is connected to a terminal device returns True, otherwise False.

5 ()

Returns the file the next line.

6 ([size])

Reads the specified number of bytes from the file, if not given or is negative read all.


file.readline ([size])

Read the entire line, including the "\ n" character.


file.readlines ([sizehint])

Read all rows and return a list, if given sizeint> 0, returns the sum of approximately sizeint byte row of the actual read value may be higher than sizhint larger, because of the need to fill the buffer.

9 (offset [, whence])

Current position settings file


file.tell ()

Returns the current file position.


file.truncate ([size])

Intercept file intercepted by byte size specified, it defaults to the current file position.


file.write (str)

The string to the file, there is no return value.


file.writelines (sequence)

Write a list of strings to the file sequence, if you need to change the line will have its own line breaks added for each row.