Python3 open() Function
The Python open() function is used to open a file and returns 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() function, you must ensure that the file object is closed, that is, call the close() function.
The open() function commonly takes two parameters: file name (file) and 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, file path (relative or absolute path).
- mode: Optional, file opening mode
- buffering: Set buffering
- encoding: Usually use utf8
- errors: Error 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:
| Mode | Description |
|---|---|
| t | Text mode (default). |
| x | Write mode, creates a new file. If the file already exists, an error will be reported. |
| b | Binary mode. |
| + | Open a file for updating (readable and writable). |
| U | Universal newline mode (not recommended). |
| r | Open the file read-only. The file pointer 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. Generally used for non-text files such as images. |
| 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. |
| w | Open a file for writing only. If the file already exists, open the file and edit from the beginning, meaning the original content will be deleted. If the file does not exist, create a new file. |
| wb | Open a file in binary format for writing only. If the file already exists, open the file and edit from the beginning, meaning 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. |
| w+ | Open a file for reading and writing. If the file already exists, open the file and edit from the beginning, meaning 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, open the file and edit from the beginning, meaning 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. |
| a | Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, new content will be written after the existing content. If the file does not exist, create a new file for 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. That is, new content will be written after the 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. |
Default is text mode. To open in binary mode, addb 。
Example
Test file test.txt, content is as follows:
EXAMPLE1 EXAMPLE2
>>>f = open('test.txt')
>>> f.read()
'EXAMPLE1\nEXAMPLE2\n'
Python3 Built-in Functions