Python3 File methods
The open() method
The Python open() method opens a file and returns a file object.
This function is needed whenever a file is processed; 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:
- file: required, the file path (relative or absolute).
- mode: optional, the file opening mode
- buffering: sets buffering
- encoding: generally utf8
- errors: error reporting level
- newline: distinguishes newline characters
- closefd: the type of the file parameter passed in
- opener: sets a custom opener; the opener's return value must be an open file descriptor.
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 (Python 3 does not support)。 |
| 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 |
Closes the file. After closing, the file can no longer be read or written. |
| 2 |
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 |
Returns an integer file descriptor (FD), which can be used for low-level operations such as the read method of the os module. |
| 4 |
Returns True if the file is connected to a terminal device, otherwise False. |
| 5 |
The File object in Python 3 does 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 everything. |
| 7 |
Reads a whole line, including the "\n" character. |
| 8 |
Reads all lines and returns a list; if sizeint>0 is given, returns lines totaling roughly sizeint bytes, though the actual amount read may be larger because the buffer must be filled. |
| 9 |
Moves the file read pointer to the specified position |
| 10 |
Returns the current position in the file. |
| 11 |
Truncates the file starting from the first character of the first line to size characters; without size, truncation starts at the current position. All characters after the truncation are deleted; on Windows a newline counts as 2 characters. |
| 12 |
Writes a string to the file and returns the number of characters written. |
| 13 |
Writes a sequence of strings to the file; if newlines are needed, you must add the newline character for each line yourself. |