Python File Methods

open() Method

The Python open() method is used to open a file and return a file object. This function is needed when processing files. If the file cannot be opened, an OSError will be raised.

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

The common form of the open() function is to receive two parameters: filename (file) and mode.

open(file, mode='r')

The full syntax format is:

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

Parameter description:

  • file: Required, file path (relative or absolute path).
  • mode: Optional, file opening mode
  • buffering: Set buffering
  • encoding: Generally use utf8
  • errors: Error reporting level
  • newline: Distinguish newline characters
  • closefd: Type of the file parameter passed in
  • opener: Set a custom opener; the return value of the opener must be an opened file descriptor.

The mode parameters are:

ModeDescription
tText mode (default).
xWrite mode, creates a new file; if the file already exists, an error is raised.
bBinary mode.
+Open a file for updating (readable and writable).
UUniversal newline mode (not recommended).
rOpen the file in read-only mode. The file pointer will be placed at the beginning of the file. This is the default mode.
rbOpen a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. Generally used for non-text files such as images, etc.
r+Open a file for reading and writing. The file pointer will be placed at the beginning of the file.
rb+Open a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file. Generally used for non-text files such as images, etc.
wOpen a file for writing only. If the file already exists, it is opened and edited from the beginning, i.e., the original content will be deleted. If the file does not exist, create a new file.
wbOpen a file in binary format for writing only. If the file already exists, it is opened and edited from the beginning, i.e., the original content will be deleted. If the file does not exist, create a new file. Generally used for non-text files such as images, etc.
w+Open a file for reading and writing. If the file already exists, it is opened and edited from the beginning, i.e., the original content will be deleted. If the file does not exist, create a new file.
wb+Open a file in binary format for reading and writing. If the file already exists, it is opened and edited from the beginning, i.e., the original content will be deleted. If the file does not exist, create a new file. Generally used for non-text files such as images, etc.
aOpen a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, new content will be written after existing content. If the file does not exist, create a new file for writing.
abOpen a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, new content will be written after existing content. If the file does not exist, create a new file for writing.
a+Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. The file will be opened in append mode. If the file does not exist, create a new file for reading and writing.
ab+Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file for reading and writing.

The default is text mode. To open in binary mode, addb 。

file object

A file object is created using the open function. The following table lists common functions of the file object:

No.Method and Description
1

file.close()

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

2

file.flush()

Flush the internal buffer of the file, directly writing the data in the internal buffer to the file immediately, rather than passively waiting for the output buffer to be written.

3

file.fileno()

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

4

file.isatty()

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

5

file.next()

Return the next line of the file.

6

file.read([size])

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

7

file.readline([size])

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

8

file.readlines([sizeint])

Read all lines and return a list. If sizeint>0 is given, it sets how many bytes to read at a time, in order to reduce reading pressure.

9

file.seek(offset[, whence])

Set the current position of the file.

10

file.tell()

Return the current position of the file.

11

file.truncate([size])

Truncate the file; the number of bytes to truncate is specified by size, defaulting to the current file position.

12

file.write(str)

Write a string to the file; the length of the written characters is returned.

13

file.writelines(sequence)

Write a sequence string list to the file. If line breaks are needed, add the newline character for each line yourself.

Other extensions