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

Python File methods

Python File methods

The open() method

The Python open() method is used to open a file and returns a file object; this function is needed throughout file processing. If the file cannot be opened, an OSError is raised.

**Note:** when using open(), always make sure to close the file object, i.e. call the close() method.

The common form of open() takes two arguments: the file name (file) and the mode (mode).

open(file, mode='r')

The full syntax is:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Parameter description:

The mode parameter has:

Mode Description
t Text mode (default).
x Write mode, creates a new file; if the file already exists, an error is raised.
b Binary mode.
+ Opens a file for updating (both reading and writing).
U Universal newline mode (not recommended).
r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
rb Opens a file in binary format for reading only. The file pointer is placed at the beginning of the file. This is the default mode. Generally used for non-text files such as images.
r+ Opens a file for both reading and writing. The file pointer is placed at the beginning of the file.
rb+ Opens a file in binary format for both reading and writing. The file pointer is placed at the beginning of the file. Generally used for non-text files such as images.
w Opens a file for writing only. If the file exists it is opened and editing starts from the beginning, so the original contents are deleted. If the file does not exist, a new file is created.
wb Opens a file in binary format for writing only. If the file exists it is opened and editing starts from the beginning, so the original contents are deleted. If the file does not exist, a new file is created. Generally used for non-text files such as images.
w+ Opens a file for both reading and writing. If the file exists it is opened and editing starts from the beginning, so the original contents are deleted. If the file does not exist, a new file is created.
wb+ Opens a file in binary format for both reading and writing. If the file exists it is opened and editing starts from the beginning, so the original contents are deleted. If the file does not exist, a new file is created. Generally used for non-text files such as images.
a Opens a file for appending. If the file exists, the file pointer is placed at the end of the file, so new content is written after the existing content. If the file does not exist, a new file is created for writing.
ab Opens a file in binary format for appending. If the file exists, the file pointer is placed at the end of the file, so new content is written after the existing content. If the file does not exist, a new file is created for writing.
a+ Opens a file for both reading and writing. If the file exists, the file pointer is placed at the end of the file and the file is opened in append mode. If the file does not exist, a new file is created for reading and writing.
ab+ Opens a file in binary format for appending. If the file exists, the file pointer is placed at the end of the file. If the file does not exist, a new file is created for reading and writing.

The default is text mode; to open in binary mode, add b .

The file object

A file object is created with the open function; the table below lists commonly used functions of file objects:

No. Method and description
1

file.close()

Closes the file. After closing, the file can no longer be read or written.

2

file.flush()

Flushes the file's internal buffer, writing the buffered data to the file immediately instead of passively waiting for the output buffer to be written.

3

file.fileno()

Returns an integer file descriptor (FD), which can be used for low-level operations such as the read method of the os module.

4

file.isatty()

Returns True if the file is connected to a terminal device, otherwise False.

5

file.next()

Returns the next line of the file.

6

file.read([size])

Reads the specified number of bytes from the file; if not given or negative, reads everything.

7

file.readline([size])

Reads a whole line, including the "\n" character.

8

file.readlines([sizeint])

Reads all lines and returns a list. If sizeint>0 is given, it sets how many bytes to read at a time, which reduces the reading pressure.

9

file.seek(offset[, whence])

Sets the current position of the file.

10

file.tell()

Returns the current position in the file.

11

file.truncate([size])

Truncates the file; the number of bytes to truncate is specified by size, which defaults to the current file position.

12

file.write(str)

Writes a string to the file and returns the number of characters written.

13

file.writelines(sequence)

Writes a sequence of strings to the file; if newlines are needed, you must add the newline character for each line yourself.