SoFunction
Updated on 2024-11-10

Organizing Python's File Methods

open() method

The Python open() method is used to open a file and return a file object, which is used during file processing, and throws an OSError if the file cannot be opened.

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

The common form of the open() function takes two arguments: the file name (file) and the mode (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, the path to the file (relative or absolute).
  • mode: optional, file opening mode
  • buffering: set buffering
  • encoding: usually utf8
  • errors: error level
  • newline: Distinguish line breaks
  • closefd: type of incoming file parameter
  • opener:
  • mode

The parameters are:

paradigm descriptive
t Text mode (default).
x Write mode, creates a new file and reports an error if the file already exists.
b Binary mode.
+ Open a file for updating (readable and writable).
U Generic line feed mode (not recommended).
r Open the file in read-only mode. The pointer to the file will be placed at the beginning of the file. This is the default mode.
rb Open 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. It is generally used for non-text files such as images.
r+ Opens a file for reading and writing. The file pointer will be placed at the beginning of the file.
rb+ Opens 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.
w Opens a file for writing only. If the file already exists then the file is opened and edited from the beginning, i.e. the original content is deleted. If the file does not exist, creates a new file.
wb Opens a file in binary format for writing only. If the file already exists then the file is opened and edited from the beginning, i.e. the original content is deleted. If the file does not exist, creates a new file. Generally used for non-text files such as images.
w+ Opens a file for reading and writing. If the file already exists then the file is opened and edited from the beginning, i.e. the original content is deleted. If the file does not exist, a new file is created.
wb+ Opens a file in binary format for reading and writing. If the file already exists the file is opened and edited from the beginning, i.e. the original content is 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 already exists, the file pointer will be placed at the end of the file. That is, the new content will be 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 already exists, the file pointer will be placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing.
a+ Opens 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 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 already exists, the file pointer will be placed at the end of the file. If the file does not exist, a new file is created for reading and writing.

Defaults to text mode, add b if you want to open in binary mode.

file object

The file object is created using the open function. The following table lists the commonly used functions for the file object:

serial number Methodology and description
1

()

Close the file. The file can no longer be read or written after closing.

2

()

Flush the internal buffer of the file, directly write the data in the internal buffer to the file immediately, instead of passively waiting for the output buffer to be written.

3

()

Returns an integer file descriptor (file descriptor FD integer), which can be used in some underlying operations such as the read method of the os module.

4

()

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

5

()

Returns the next line of the file.

6

([size])

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

7

([size])

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

8

([sizeint])

Read all lines and return the list, if given sizeint>0, is to set how many bytes to read at a time, this is to reduce the reading pressure.

9

(offset[, whence])

Setting the current location of the file

10

()

Returns the current position of the file.

11

([size])

Intercept the file, the bytes to be intercepted are specified by size, which defaults to the current file location.

12

(str)

Writes a string to a file, the return is the length of the characters written.

13

(sequence)

Writes a list of sequential strings to the file, adding line breaks to each line itself if they are needed.