You can open internal files in Python with the help of a simple function, open()
. Here we will learn how to use this function in Python.
Open() in Python
The open()
function in Python opens the internally stored files and returns the content of the corresponding file as a Python object.
Syntax of Open() in Python:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Parameters:
- file: It is the path name of the file that we want to open.
- mode: It is a string that defines in which mode the file is opened, but if the mode is not provided, it takes up the default value of 'r'.
- buffering: It is an optional parameter used to set buffering policy.
- encoding: It is another optional parameter used to encode the format.
- errors: It is also an optional parameter that specifies how to handle encoding and decoding errors.
- newline: This string tells how newlines mode works (values: None, '', '\n', '\r', and '\r\n').
- closefd: This value must be TRUE (also the default value), otherwise an exception is raised.
- opener: It is a custom opener and must return an open file descriptor.
The other strings that can be used are given as follows:
- 'r': This string is used to read (only) the file. It is passed as default if no parameter is supplied and returns an error if no such file exists.
- 'rb': This string is used to read (only) the file in binary format.
- 'r+': This string is used to both read and write the file.
- 'rb+': This string is used to both read and write the file in binary format.
- 'w': This string is used for writing on/over the file. If the file with the supplied name doesn’t exist, it creates one for you.
- 'w+': This string is used for both reading and writing on/over the file.
- 'wb': This string is used for writing on/over the file in binary format.
- 'wb+': This string is used for both reading and writing on/over the file in binary format.
- 'a': This string is used to add (append) content to an existing file. If no such file exists, it creates one for you.
- 'ab': This string is used to add (append) content to an existing file in binary format.
- 'ab+': This string is used to add (append) and read content to an existing file in binary format.
- 'a+': This string is used to add (append) and read content to an existing file.
- 'x': This string is used to create a specific file.
- 'b': This string is used when the user wants to handle the file in binary mode. This is generally used to handle image files.
- 't': This string is used to handle files in text mode. By default, the
open()
function uses the text mode.
Code and Explanation
Remember that the open()
function returns an internal file that is used to read, write, and add content to the file, but if the file you want to open doesn't exist then it throws a FileNotFoundError error.
Input:
# Open file to read
file = open(file_path, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
print(file.read() == False)
# Open file to write
file = open(file_path, mode='w', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
print(file.write("Writing to file") == False)
# Open file to append
file = open(file_path, mode='a', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
print(file.write("Appending to file") == False)
Output:
True
True
True
Closing thoughts
The open()
function in Python lets you open any internal file if it exists which can be read, written, or appended according to the user's need. The different string values in mode let you perform those functions as per your need. One can learn about more Python concepts here.