Python3 open() Function
The Python open() function is used to open a file and return a file object. This function is essential for processing files. If the file cannot be opened, an OSError is raised.
Note: It is crucial to ensure that the file object is closed using the close() function after using open().
The common form of the open() function accepts two parameters: the file name (file) and the mode (mode).
open(file, mode='r')
The complete syntax is:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Parameter descriptions:
- file: Required, the file path (relative or absolute).
- mode: Optional, the file open mode.
- buffering: Sets the buffering policy.
- encoding: Typically uses UTF-8.
- errors: Error handling level.
- newline: Distinguishes newline characters.
- closefd: Type of the file parameter passed.
- opener:
The mode parameter options are:
Mode | Description |
---|---|
t | Text mode (default). |
x | Write mode, creates a new file. If the file already exists, an error is thrown. |
b | Binary mode. |
+ | Opens a file for updating (read and write). |
U | Universal newline mode (deprecated). |
r | Opens a file for reading. 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. The file pointer is placed at the beginning of the file. This is the default mode. Typically used for non-text files like 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. Typically used for non-text files like images. |
w | Opens a file for writing only. If the file already exists, it opens the file and starts editing from the beginning, deleting the existing content. If the file does not exist, a new file is created. |
wb | Opens a file in binary format for writing only. If the file already exists, it opens the file and starts editing from the beginning, deleting the existing content. If the file does not exist, a new file is created. Typically used for non-text files like images. |
w+ | Opens a file for both reading and writing. If the file already exists, it opens the file and starts editing from the beginning, deleting the existing content. 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 already exists, it opens the file and starts editing from the beginning, deleting the existing content. If the file does not exist, a new file is created. Typically 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. 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 already exists, the file pointer is placed at the end of the file. 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 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
.
Example
Test file test.txt, with the following content:
tutorialpro1
tutorialpro2
>>> f = open('test.txt')
>>> f.read()
'tutorialpro1\ntutorialpro2\n'