Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 open() Function

Python3 open() Function

Python Built-in Functions Python3 built-in function

The Python open() function opens a file and returns a file object; it is needed whenever a file is processed, and 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() function.

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:

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 (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 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 .

Example

The test file test.txt has the following contents:


RUNOOB1
RUNOOB2

f

=

open

(

'

test.txt

'

)

f

.

read

(

)

'

RUNOOB1

\n

RUNOOB2

\n

'

Python Built-in Functions Python3 built-in function