This is a detailed tutorial of the Python built-in open() Function. Learn to open any kind of files in Python using this method with the help of examples.
The Python built-in function
open(), if possible, tries to open a file with a given name and returns its file object.
Given below is the syntax of the
fileObject = open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
This function can take up to 8 different arguments that are described below.
- file. This is a string type argument that contains the path of the file to open with the extension.
- mode. This optional argument defines the mode in which you want to open the file. There are a number of modes available for the Python
open()function as described below. The default value of this argument is set to
'r'that refers to the reading mode.
||Reading Mode. You’ll only be able to read the file contents via the file object so returned. You will not able to modify any of the file contents.|
||Writing Mode. This mode is used to write the content into the file. In case, the file with the given name does not exist for the given path, the function, when called with the writing mode, will create one.
In case, the file already exists, even with some content, in this case, the already existing content will be truncated.
||Exclusive Creation Mode. This mode opens the file for exclusive creation. In case the file with the given name already exists, this operation will fail.|
||Append Mode. This mode opens the file to add more content to it after the existing content. This will not truncate the file if it already exists but will create one if it does not exists.|
||Update Mode. (Used for both reading and writing)|
- buffering. This is also an optional parameter and is used to set up the buffering policy.
- encoding. This optional parameter defines the encoding to follow to fetch the file contents. The default encoding is
- errors. This is a string type argument using which we can tell this function to how to handle errors. It is similar to the
errorsargument of the built-in function str().
- newline. This optional parameter defines what are the symbols to follow to identify the newlines in the given file. You can specify the values like,
- closefd. This is another optional boolean type parameter. The default value of this argument is True. The function simply raises an exception if this is set to
- opener. Again, an optional parameter to specify open file descriptor.
open() Return Value
open() returns a file object. This object can be used to perform a number of operations on the file like Reading, Writing, and Modification.
Note. If you’re opening a file in reading mode and file does not exist, it raises the
Given below are the two examples illustrating the usage of the Python built-in
Example 1. Basic Usage of the
Let’s say we have a file named
wtmatter.txt in the current direct same as the location of our python file running the code. The following code snippet shows how to open this file, simply for reading as it’s the default mode is
'r'. We’ve not specified the mode explicitly as the second argument, that’s why it will assume it automatically to be the reading mode, considering it’s the default argument value.
#If File Exists in the same directory as the Python File Running this code
fileObject = open("wtmatter.txt")
#If in another directory
#Specify Full Path
fileObject = open("C:\Users\Gurmeet\Desktop\wtmatter.txt")
This will not print anything to the console. You’ve to explicitly perform several actions to read this file. I have linked below two of my articles that will help you in this regard.
Example 2. Opening files in different modes & encoding using
You can specify appropriate modes and encoding to perform the operations on a given file correctly. That’s what is illustrated in the following example.
#Opening a file in reading mode
file = open('wtmatter.txt', mode='r')
#Opening a file in writing mode
#File Encoding set to UTF-8
file = open('blog.txt', mode='w', encoding='utf-8')
#Exclusively Creating a new file
#It should not exist before
file = open('hello.txt', mode='x')
#Opening a file in append mode
file = open('blog.txt', mode='a')
If you do not set any encoding in your Python file, it automatically assumes the encoding to be ASCII, as that’s the default encoding set for Python, although the argument
encoding has the default value set to None.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!