Python open() Method
The open()
method opens the file (if possible) and returns the corresponding file object.
open() Syntax:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Parameters:
- file: The path name of the file to be opened or an integer file descriptor of the file to be wrapped.
- mode: (Optional) An access mode while opening a file. Default mode is 'r' for reading. See all the access modes.
- buffering: (Optional) Used for setting buffering policy. Defaults to -1. Pass 0 if specified mode is binary 'b',
- encoding: (Optional) The encoding format to encode or decode the file.
- errors: (Optional) String specifying how to handle encoding/decoding errors.
- newline: (Optional) Conrols how newlines mode works. It can be None, '', '\n', '\r', and '\r\n'.
- Closefd: (Optional) If a filename is given, it must be True. If False, the file descriptor will be kept open when the file is close
- Opener: (Optional) A custom callable file opener.
Return type:
Returns a file object.
File Access Modes
The following table lists the file access modes parameter values.
Access Modes | Description |
---|---|
r | Opens a file for reading only. |
rb | Opens a file for reading only in binary format. |
r+ | Opens a file for both reading and writing. |
rb+ | Opens a file for both reading and writing in binary format. |
w | Opens a file for writing only. |
wb | Opens a file for writing only in binary format. |
w+ | Opens a file for both writing and reading. |
wb+ | Opens a file for both writing and reading in binary format. |
a | Opens a file for appending. |
ab | Opens a file for appending in binary format. |
a+ | Opens a file for both appending and reading. |
ab+ | Opens a file for both appending and reading in binary format. |
The following example opens a file from the current directory for reading and writing to file. The current directory in Python shell is C:\python38
.
>>> f = open("MyFile.txt") # Open MyFile.txt from the current directory for reading
>>> f = open("MyFile.txt", 'w') # Open MyFile.txt from the current directory for writing
# perform file operations using f
>>> f.close()
The file operations can raise exceptions if something goes wrong. So, it is recommended to use try-except-finally to handle exceptions and closing a file object.
try:
f = open("MyFile.txt") # Open MyFile.txt from the current directory for reading
# perform file operations using f
except:
# handle exceptions
finally:
f.close()
The following example opens files for read, write or update operations.
>>> f = open("C:\MyFile.txt") # open for reading
>>> f = open("C:\MyFile.txt",'w') # open for writing
>>> f = open("C:\MyFile.txt", 'x') # open for exclusive creation
>>> f = open("C:\MyFile.txt", 'a') # open for writing, appending to the end of file
>>> f = open("C:\MyApp.exe", 'b') # open in binary mode for read
>>> f = open("C:\myimage.jpg", 'rb') # open file in binary mode for read and write
>>> f = open("C:\MyFile.txt", 't') # open in text mode for read
>>> f = open("C:\MyFile.txt", mode='r', encoding='utf-8') # open for reading with utf-8 encoding
>>> f = open("C:\MyFile.txt", '+') # open for reading or writing
>>> print(type(f)
<class '_io.TextIOWrapper'>
>>> f.close() # closing file after file operations
Visit working with files in Python for more information.