Python3 File Methods
open() Method
Section titled “open() Method”Python’s open() method is used to open a file and return a file object.
This function is required for all file processing. If the file cannot be opened, an OSError is thrown.
Note: When using the open() method, you must ensure the file object is closed, i.e., call the close() method.
The open() function commonly takes two parameters: file name (file) and mode (mode).
The complete syntax format is:
Parameter descriptions:
- file: Required, the file path (relative or absolute path).
- mode: Optional, the file open mode
- buffering: Sets the buffer
- encoding: Generally use utf8
- errors: Error level
- newline: Distinguishes newline characters
- closefd: Type of the file parameter passed in
- opener: Sets a custom opener; the return value of the opener must be an open file descriptor.
The mode parameter values:
| Mode | Description |
| t | Text mode (default). |
| x | Write mode, creates a new file. If the file already exists, an error is reported. |
| b | Binary mode. |
| + | Opens a file for updating (read and write). |
| U | Universal newline mode (not supported in Python 3). |
| r | Opens the file in read-only mode. The file pointer is placed at the beginning of the file. This is the default mode. |
| rb | Opens a file in binary format for read-only. The file pointer is placed at the beginning of the file. This is the default mode. Generally used for non-text files like images. |
| r+ | Opens a file for reading and writing. The file pointer is placed at the beginning of the file. |
| rb+ | Opens a file in binary format for reading and writing. The file pointer is placed at the beginning of the file. Generally used for non-text files like images. |
| w | Opens a file for write-only. If the file already exists, it opens the file and starts editing from the beginning, i.e., the original content will be deleted. If the file does not exist, a new file is created. |
| wb | Opens a file in binary format for write-only. If the file already exists, it opens the file and starts editing from the beginning, i.e., the original content will be deleted. If the file does not exist, a new file is created. Generally used for non-text files like images. |
| w+ | Opens a file for reading and writing. If the file already exists, it opens the file and starts editing from the beginning, i.e., the original content will be 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, it opens the file and starts editing from the beginning, i.e., the original content will be deleted. If the file does not exist, a new file is created. Generally used for non-text files like images. |
| a | Opens a file for appending. If the file already exists, the file pointer is placed at the end of the file. That is, new content will be written after 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 is placed at the end of the file. That is, new content will be written after 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 is placed at the end of the file. The file opens 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 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.
file object
Section titled “file object”The file object is created using the open function. The table below lists the commonly used functions of the file object:
| No. | Method and Description |
|---|---|
| 1 |
Closes the file. After closing, the file can no longer be read from or written to. |
| 2 |
Flushes the internal buffer, immediately writing the data in the internal buffer to the file, rather than 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 low-level operations such as the os module's read method. |
| 4 |
Returns True if the file is connected to a terminal device, otherwise returns False. |
| 5 |
File objects in Python 3 do not support the next() method. Returns the next line of the file. |
| 6 |
Reads the specified number of bytes from the file. If not given or negative, reads all. |
| 7 |
Reads an entire line, including the "\n" character. |
| 8 |
Reads all lines and returns a list. If sizeint>0 is given, returns lines whose total is approximately sizeint bytes. The actual read value may be larger than sizeint because the buffer needs to be filled. |
| 9 |
Moves the file read pointer to the specified position |
| 10 |
Returns the current position of the file. |
| 11 |
Truncates from the first character of the first line of the file, truncating the file to size characters. Without size, it truncates from the current position; after truncation, all subsequent characters are deleted. On Windows systems, newlines represent 2 characters in size. |
| 12 |
Writes a string to the file, returning the length of characters written. |
| 13 |
Writes a list of sequence strings to the file. If you need newlines, you must add the newline character for each line yourself. |