Python3 Func Open
# Python3.x Python3 open() Function
[ Python3 Built-in Functions](#)
The Python open() function is used to open a file and returns a file object. This function is required for all file processing operations. If the file cannot be opened, an OSError will be raised.
**Note:** When using the open() function, you must ensure the file object is closed, i.e., by calling the close() function.
The common form of the open() function accepts two parameters: the file name (file) and the mode (mode).
```python
open(file, mode='r')
The complete syntax is:
```python
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 the buffering.
* encoding: Generally use 'utf8'.
* errors: The error reporting level.
* newline: Distinguishes line endings.
* closefd: The type of the passed file parameter.
* opener: Sets a custom opener. The opener's return value must be an open file descriptor.
The mode parameter options are:
| Mode | Description |
| --- | --- |
| t | Text mode (default). |
| x | Write mode, creates a new file. Raises an error if the file already exists. |
| b | Binary mode. |
| + | Opens a file for updating (readable and writable). |
| U | Universal newline mode (not recommended). |
| 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 for reading only in binary format. 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 both reading and writing. The file pointer is placed at the beginning of the file. |
| rb+ | Opens a file for both reading and writing in binary format. The file pointer is placed at the beginning of the file. Generally used for non-text files like images. |
| w | Opens a file for writing only. If the file exists, it is opened and truncated from the beginning (existing content is deleted). If the file does not exist, a new file is created. |
| wb | Opens a file for writing only in binary format. If the file exists, it is opened and truncated from the beginning (existing content is 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 both reading and writing. If the file exists, it is opened and truncated from the beginning (existing content is deleted). If the file does not exist, a new file is created. |
| wb+ | Opens a file for both reading and writing in binary format. If the file exists, it is opened and truncated from the beginning (existing content is 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 exists, the file pointer is placed at the end of the file. 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 for appending in binary format. If the file exists, the file pointer is placed at the end of the file. 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 both reading and appending. If the file exists, the file pointer is 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 for both reading and appending in binary format. 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'.
### Example
Test file `test.txt`, content as follows:
TUTORIAL1
TUTORIAL2
```python
>>> f = open('test.txt')
>>> f.read()
'TUTORIAL1nTUTORIAL2n'
[ Python3 Built-in Functions](#)
[](#)(#)
(#)[](#)
YouTip