Python open() Function

Python Built In Open() Function

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.

Python open() Function

The Python built-in function open(), if possible, tries to open a file with a given name and returns its file object.

Syntax

Given below is the syntax of the open() function.

open() Parameters

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.
Mode Description
r 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.
w 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.

x 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.
a 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.
t Text Mode.
b Binary Mode.
+ 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 None.
  • errors. This is a string type argument using which we can tell this function to how to handle errors. It is similar to the errors argument 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, None, ' ', '\n', 'r', and '\r\n'.
  • 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 False.
  • opener. Again, an optional parameter to specify open file descriptor.

open() Return Value

The function 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 FileNotFoundError exception.

Examples

Given below are the two examples illustrating the usage of the Python built-in open() function.

Example 1. Basic Usage of the open() function

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.

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 open()

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.

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!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *