Python open() function is used to open a file. This is the first step while working with files. Whether we want to read, write or edit files data, we first need to open it using open() function.
Python open() function syntax is:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
- file: specifies the file path object. Usually, a str or bytes object representing the file path. This is a mandatory argument.
- mode: specifies the file opening mode. There are different modes to open a file.
- r: opens file in read-only mode.
- w: opens the file in write mode, the file is truncated.
- x: open for exclusive creation, failing if the file already exists
- a: open for writing, appending to the end of the file if it exists
- b: binary mode
- t: text mode (default)
- +: open a disk file for updating (reading and writing)
File opened in binary mode return content of file as bytes without any decoding. Whereas files opened in text mode contents are returned as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding.
- buffering: optional integer specifying the buffering policy. If passed as 0, buffering is turned off. This is allowed only when files are opened in binary mode. If passed as 1, line buffering is used and it’s allowed only in text mode. If passed greater than 1, then bytes of a fixed-size chunk buffer of specified size is used.
- encoding: name of the encoding used to decode or encode the file. It should be used only in text mode.
- errors: an optional string that specifies how encoding and decoding errors are to be handled, this cannot be used in binary mode. Some of the standard values are strict, ignore, replace etc.
- newline: this parameter controls how universal newlines mode works (it only applies to text mode). It can be None, ”, ‘\n’, ‘\r’, and ‘\r\n’.
- opener: A custom opener can be used by passing a callable as opener.
Most of the time, we use only file and mode parameters to open a file and perform necessary actions on it.
When a file is opened in text mode,
TextIOWrapper instance is returned. When the file is opened in binary mode,
BufferedRandom instance is returned.
Python Open File
Let’s look at some examples of opening file in python.
Open File in Text and Read Only Mode
# open file in text and read only mode f = open('data.txt', mode='r') print(type(f)) f.close()
Open File in Binary and Read Only Mode
f = open('favicon.ico', mode='r+b') print(type(f)) f.close()
Open file in binary mode, read only and buffer
f = open('favicon.ico', mode='br', buffering=16) f.close()
Open file in binary mode, read-only and no buffering
f = open('favicon.ico', mode='br', buffering=0) f.close()
Open file in text mode, read-only and line buffering
f = open('data.txt', mode='a', buffering=1) f.close()
Open text file in write mode with truncate
f = open('data.txt', mode='w') f.close()
Open file in exclusive creation mode
If the file already exists, passing ‘x’ as mode will throw FileExistsError. We can use try except block to catch this exception and perform corrective actions.
try: f = open('data.txt', mode='x') except FileExistsError as e: print('file already exists')
file already exists
That’s all for opening file in python, for more file related examples go through Python File Handling.
Reference: Official Documentation