codescracker
python

Python open() Function



« Python Tutorial Python Examples »

The open() function is the most used function in file handling using Python. Therefore, do concentrate on this tutorial, that will teach you everything about this function along with many examples. I've covered all the topics related to open() in Python. Let's start with its syntax.

Python open() Syntax

To open a file in Python, we need to use open() function. Here is the syntax:

open(filename, mode, buffering, encoding, errors, newline, closefd, opener)

Note - Out of all 8 parameters of open() function, only the first two parameters we need in general. Rest of all 6 parameters are not needed at most of the time.

Last 6 parameters are optional. Even the second parameter, i.e. mode can also be considered as optional, because if you will not provide the mode, then it is treated as r, since r is the default file opening mode.

Here is the demo codes, uses open() function to open a file codescracker.txt in reading mode. I'm not using all the 6 optional parameters in this code:

open("codescracker.txt", "r")

The open() function returns file object, if file is available. Otherwise produces FileNotFoundError error. I've created that types of programs also, that can handle these type of errors.

Description of all Parameters of open() Function

The table given below describes all the parameters of open() function in Python. The description of parameters are given in brief or in a one-line description. But I've described along with example about all parameters, below the table, one by one.

Parameter Description Default Value
filename The name of file to open. No default value, we need to pass the name of file
mode It is the opening mode of the file. The default value is 'r'. It indicates to open the file in reading mode
buffering This parameter is used to set buffering policy. The default value is -1
encoding This parameter is used to provide the encoding format to encode/decode the file. The default value is None
errors This parameter is basically used to handle encoding/decoding errors The default value is None
newline This parameter helps in controlling the work of newline The default value is None
closefd The closefd stands for close file descriptor. It is True if the name of file is given. Otherwise the file descriptor will keep open when the file is closed The default value is True
opener This parameter is used to open the file in custom way. The default value is None

Python open() Example

Since the function open() is used to open a file. Therefore let's first create a file named codescracker.txt. I've create and saved the file. Here is the snapshot of the opened codescracker.txt, a newly created file in current directory:

python open function

Since the file is created. Now we can proceed to operate this file. Therefore, I've create some helpful example programs for open() functions, given below.

filename Parameter in open() Function

This is the first parameter of open() function in Python. As already said, it is used to give the name of file, that is going to operate (read, write, update, delete).

Note - The name of file must be given along with its extension such as myfile.txt, myfile.html etc.

Here is an example, opens the file in reading mode:

fp = open("codescracker.txt")
if fp:
    print("The file, \"codescracker.txt\" opened successfully!")

Since the file, given in above program, is already created in the current directory. Therefore the above program produces the output as shown in the snapshot given below:

filename parameter in open function python

Since the fp is equals to True in the following code:

if fp:

from above program. Therefore program flow goes inside the if's body, and executes the print() statement. Means that, the open() function returns True, if the file is available in the directory. Otherwise, will produce the FileNotFoundError error. For example, let's create the same program, by giving a file name that does not exist in the current directory:

fp = open("none.txt")
if fp:
    print("The file, \"none.txt\" opened successfully!")

Here is the output you'll see, if the file none.txt does not exist:

python open function example

Here is another program, that shows how to handle FileNotFoundError error manually to create a good user-experience program:

try:
    fp = open("none.txt")
    print("The file, \"none.txt\" opened successfully!")
except FileNotFoundError:
    print("The file, \"none.txt\" not found!")

This program produces following output:

The file, "none.txt" not found!

Open File by User Input

Let's create a program, that receives the name of file from user at run-time of the program:

print("Enter the Name of File: ", end="")
filename = input()

try:
    fp = open(filename)
    print("The file, \"" + filename + "\" opened successfully!")
except FileNotFoundError:
    print("The file, \"" + filename + "\" not found!")
finally:
    fp.close()

Here is the initial output produced by above program, with user input codescracker.txt as name of file:

open function example in python

Note - The end skips insertion of an automatic newline using the default behavior of print().

In above program, the try...except...finally are exception handling blocks. There is a separate tutorial on exception handling. Therefore, to learn in detail, refer to its separate tutorial.

mode Parameter in open() Function

The mode parameter is basically used to provide the access mode for the file. There are many modes available in Python. Let's list out all those modes along with brief description.

Python File Opening Modes

These are the list of file opening modes available in Python:

r Mode Example

The program given below demonstrates the use of r file opening mode while opening the file.

print("Enter the Name of File: ", end="")
filename = input()

try:
    fp = open(filename, "r")
    print("\nThe file, \"" + filename + "\" opened successfully!")
    print("\n------The content is-------")
    print(fp.read())
except FileNotFoundError:
    print("\nThe file, \"" + filename + "\" not found!")
finally:
    fp.close()

Here is the output produced by above program, with user input, as same file, as created earlier:

python file r opening mode

r+ Mode Example

The example given below demonstrates the use of r+ file opening mode in Python:

print("Enter the Name of File: ", end="")
filename = input()

try:
    fp = open(filename, "r+")
    print("\nThe file, \"" + filename + "\" opened successfully!")
    print("\n------The content is-------")
    print(fp.read())
    print("\nEnter New Content to Write: ", end="")
    content = input()
    fp.write(content)
    print("\n------The New content is-------")
    fp.seek(0)
    print(fp.read())
except FileNotFoundError:
    print("\nThe file, \"" + filename + "\" not found!")
finally:
    fp.close()

Here is the sample run of above program, with user input codescracker.txt as file name and I'm the new content. as the new content to write to the file:

python file r plus opening mode

Note - The seek() function in above program, is used to put the file pointer fp to the beginning of the file. So that, we can read all the content from the beginning. The 0 provided as its parameter tells, that the file pointer goes at the beginning of the file.

One more concern, you may have, is why the new content gets written on the same line at last. Therefore, to avoid or to put the new content from new line. Just add the following code or statement:

fp.write("\n")

just before the following statement:

fp.write(content)

rb Mode Example

The rb mode is used to open the file for reading mode in binary format. Means that, reading the content of file in an unchanged form. That is, the content of file gets transferred in an unchanged form. Let's take a look at the example first, then will discuss in brief about it:

print("Enter the Name of File: ", end="")
filename = input()

try:
    filehandle = open(filename, "rb")
    print("\nThe file, \"" + filename + "\" opened successfully!")
    print("\n------The content is-------")
    print(filehandle.read())
except FileNotFoundError:
    print("\nThe file, \"" + filename + "\" not found!")
finally:
    filehandle.close()

Here is its sample run with user input codescracker.txt as name of file:

python file rb opening mode

And here is the snapshot of opened file, as entered in above program's sample run:

python file rb opening mode example

Note - From above file, I've removed the content I'm the new content. from the file, that was written as new content in previous example program.

Now you can see, from the above original file's content and the content of the file, displayed by the program, after opening the file using open() in rb opening mode. That is, in binary format, the content transferred in unchanged form. Therefore, the following output:

b'Hey!\r\nThis is a file.\r\nThe name of this file is codescracker.'

was produced as the content of file. Here the first character b indicates to binary format. After b and after the content, there is an apostrophe ('). And inside this apostrophe, we'll get the content of file in unchanged form as shown above.

Note - The \r moves the cursor at the beginning of the line.

Note - The \n inserts a newline.

But in binary format, we'll see the code. We do not see the output of the code such as \r, \n etc.

Important - Opening file for reading in binary format, means the content or data read from the file will gets returned as bytes object, not str object.

Note - I do not recommend you to use this mode. As in binary mode or when a file opened for reading in binary format, means the file is not treated as lines of text, rather it is treated as a series of bytes. I've provided the example, just for your understanding on how the things works.

rb+ Mode Example

print("Enter the Name of File: ", end="")
filename = input()

try:
    fp = open(filename, "rb+")
    print("\nThe file, \"" + filename + "\" opened successfully!")
    print("\n------The content is-------")
    print(fp.read())
    print("\nEnter New Content to Write: ", end="")
    content = input()
    fp.write(content.encode('utf-8'))
    print("\n------The New content is-------")
    fp.seek(0)
    print(fp.read())
except FileNotFoundError:
    print("\nThe file, \"" + filename + "\" not found!")
finally:
    fp.close()

Here is its sample run with user input, codescracker.txt as file name and I'm the new content. as the new content to write:

python file rb plus opening mode

Note - In the second read of file, the content gets placed in double quote, because of I'm the new content, that is an apostrophe between the first character I and the third character m of the string.

In above program, the following code or statement:

fp.write(content.encode('utf-8'))

is used to encode the string stored in content variable into the UTF-8 format. So as to avoid error while putting the content after opening the file for reading and writing both in binary format using rb+ file opening mode. Because, if you use the normal statement for writing like:

fp.write(content)

then it'll produce an error, that is a TypeError, that looks like:

python file rb plus opening mode example

w Mode Example

print("Enter the Name of File: ", end="")
filename = input()

try:
    fp = open(filename, "w")
    print("\nEnter the Content to Write: ", end="")
    content = input()
    fp.write(content)
finally:
    fp.close()
    print("\nThe content written on the file.")

Here is its sample run with filename as same file as of previous program, and I'm the new content. as content to write:

python file w opening mode

And here is the snapshot of the directly opened file, after executing the above program:

python file w opening mode example

Point to Note - The previous content gets removed if we opened the file in w mode. We need to use a in place of w. So that, the previous content does not gets deleted.

The above program, does not provide a good user experience at all. Therefore, I've modified the above program, that gives or provides a good user experience:

print("Enter the Name of File: ", end="")
filename = input()

try:
    fp = open(filename, "r")
    print("\n------The content is------")
    print(fp.read())
    fp.close()
    fp = open(filename, "w")
    print("\nEnter the Content to Write: ", end="")
    content = input()
    fp.write(content)
    fp.close()
    fp = open(filename, "r")
    print("\n------The New content is-------")
    print(fp.read())
except FileNotFoundError:
    print("\nThe file, \"" + filename + "\" not found!")
    print("Creating the file...")
    fp = open(filename, "w")
    print("The file created successfully.")
    print("\nEnter the Content to Write: ", end="")
    content = input()
    fp.write(content)
    fp.close()
    fp = open(filename, "r")
    print("\n------The content is-------")
    print(fp.read())
finally:
    fp.close()

Here is the sample run with codescracker.txt as file name, and Hey, are you feeling good ? as content to write:

python file w mode example

And here is another sample run with user input none.txt (a file that is not available) as file name, and the content as same as previous or above sample run:

python file w open mode example

If you open the current directory, the directory where your above Python source code is saved. Then there, you'll see the none.txt file created. Here is the snapshot:

python w open mode file example

w+ Mode Example

This program can be created in similar way as done in r+ mode for reading only. If you combine and modify the program given in r+ and w, then you'll definitely get the program for w+ mode. Come on, do it yourself.

wb Mode Example

This program is similar to the program created in rb section. The only difference is, this section is for writing the content, and that section is for reading the content.

wb+ Mode Example

The program for this wb+ is also similar to the program given under the section rb+.

a Mode Example

The a mode is used in the place of w, when we need to write the content to file, without deleting the previous content of the file.

print("Enter the Name of File: ", end="")
filename = input()

try:
    fp = open(filename, "a")
    print("\nEnter the Content to Append: ", end="")
    content = input()
    fp.write("\n")
    fp.write(content)
finally:
    fp.close()
    print("\nThe content appended on the file.")

Here is its sample run, with again same file name as created at earlier, and Yes, Of course as the content to append:

python file a mode

Note - I recommend you to use the statement, fp.write("\n") before the statement that is used to append the content. Because, we are going to append, therefore appending the content from newline is better.

Now If you open the file, then you'll see as shown in the snapshot given below:

python file a open mode example

See, the content is appended, from the newline. To create a program that provides good user experience, then modify the second program from the section w mode. The modification is, only that, changing the w to a.

a+ Mode Example

This mode is similar to a mode, with extra feature, that provides to read the file too along with appending.

print("Enter the Name of File: ", end="")
filename = input()

try:
    fp = open(filename, "a+")
    print("\n------The content is------")
    fp.seek(0)
    print(fp.read())
    print("\nEnter the Content to Append: ", end="")
    content = input()
    fp.write("\n")
    fp.write(content)
    print("\n------The New content is-------")
    fp.seek(0)
    print(fp.read())
finally:
    fp.close()
    print("\nDone! Closing the Program.")

Here is its sample run with same file name and Thank You! as content to append:

python file a plus mode

You can play more with files, yourself. These are just some of the demo programs, providing you the idea, how the things going inside.

ab Mode Example

This program can be created easily, if you read the program provided in the section rb mode and a mode.

ab+ Mode Example

This program can also be created, if you combine and modify the program given in rb+ and a section.

x Mode Example

print("Enter the Name of File: ", end="")
filename = input()

try:
    fp = open(filename, "x")
    print("\nThe file, \"" + filename + "\" created successfully.")
except FileExistsError:
    print("\nThe file, \"" + filename + "\" already exists.")

Here is its sample run with user input as file name that already exists:

python file x mode

And here is another sample run with file name python.html, that does not exists:

python file x open mode example

If you open the current directory, then you'll found there is a newly created file named python.html will be available in the directory.

buffering Parameter in open() Function

Providing buffering=-1 or omitting this parameter, means it will be used as system default. Whereas providing buffering=1 means, we need to transfer the chunk of data from raw OS filestream into buffer.

Note - Basically, buffering is used to process the data faster.

In normal words, data buffer is a physical memory storage's region that is used to store the data temporarily, when the data is being moved from one place to other.

Note - Data buffer plays an important role, in loading or moving the data between process. Like buffer happens while watching the video online. Therefore, instead of transferring the data into the physical storage memory, the transfer of data to the buffer provides faster load of the data.

Note - We need to set buffering equals to 1, if we want to buffer the data/file.

Check Default Buffer Size

import io

print("The default buffer size:", io.DEFAULT_BUFFER_SIZE)

The output produced by above program is:

python print default buffer size

The size 8192 or 213 referred to the number of characters or bytes. That is, 8192 characters or 8192 bytes or 8 KB is the default buffer size of the system.

Note - The purpose to use buffering parameter is to execute the code faster.

encoding Parameter in open() Function

As already told, the encoding parameter is used to encode/decode the data. The most used encoding is utf-8. Now the question is, what does it mean by encoding or decoding the data ?
The answer is, the word encoding in computer science, means converting the plain or human readable text into a coded form based on the format used. Here are the list of some popular format to encode or decode the data:

etc. In all these, utf-8 is the most used and recommended encoding format to encode or decode the data.

Note - To encode Unicode characters, we need to use utf-8.

Note - Binary, mode does not take an encoding argument. If you do so, it will throw a ValueError based error message.

The big question about the parameter encoding is, because we can store the file in normal way, that is, without using the encoding parameter to encode the data in whatever the encoding format we want, then why do we need to encode or decode the data before reading or writing or for whatever we want. Because, in modern world, our program need to be handle large variety of characters.

Therefore, Python's string type uses the Unicode standard to represent these character. Only because, all the character available in every languages are with unique code. That is, Unicode.

Note - To learn about Unicode Characters in detail, refer to its separate tutorial.

After visiting the separate article on Unicode, you'll get the complete idea about the Unicode. Let's take an example that uses encoding parameter in open() function to open a file:

print("Enter the Name of File: ", end="")
filename = input()

try:
    fp = open(filename, "r")
    print("\n------The content is------")
    print(fp.read())
    fp.close()
    fp = open(filename, "w", encoding="utf-16")
    print("\nEnter the Content to Write: ", end="")
    content = input()
    fp.write(content)
    fp.close()
    fp = open(filename, "r", encoding="utf-16")
    print("\n------The New content is-------")
    print(fp.read())
except FileNotFoundError:
    print("\nThe file, \"" + filename + "\" not found!")
    print("Creating the file...")
    fp = open(filename, "w", encoding='utf-16')
    print("The file created successfully.")
    print("\nEnter the Content to Write: ", end="")
    content = input()
    fp.write(content)
    fp.close()
    fp = open(filename, "r", encoding="utf-16")
    print("\n------The content is-------")
    print(fp.read())
finally:
    fp.close()

Here is its sample run with user input codescracker.txt as file name and New content with encoding as content to write:

python encoding parameter in open

Note - If you've written the data using encoding parameter, then don't forget to use the same encoding while reading the data back on the output screen. Otherwise, it will show the data as shown below.

That is, if you read the data, using the same file, using the program like:

fp = open("codescracker.txt", "r")
print("\n------The content is------")
print(fp.read())
fp.close()

Here is the output you'll see:

python open encoding parameter example

See the output of the content after writing the new content. Since the content is encoded using utf-16, therefore you see this output. But still, if you open the file directly, then you'll see the original content as shown in the snapshot given below:

python open function encoding parameter

But if, we explicitly encode the data before printing, that is, without using encoding parameter in open() function, as shown in the program given below. This program is created to print the Unicode values of all characters based on UTF-16 format

fp = open("codescracker.txt", "r")
print("\n------The content is------")
content = fp.read()
content = content.encode("utf-16")
print(content)
fp.close()

Here is the output that prints the Unicode values based on UTF-16 encoding format:

python encoding parameter example

As you can see, the whole output is not possible to show you in one window. Here is the complete output:

------The content is------
b'\xff\xfe\xff\x00\xfe\x00N\x00\x00\x00e\x00\x00\x00w\x00\x00\x00 \x00\x00\x00c\x00\x00\x00o\x00\x00\x00n\x00\x00\x00t\x00\x00\x00e\x00\x00\x00n\x00\x00\x00t\x00\x00\x00 \x00\x00\x00w\x00\x00\x00i\x00\x00\x00t\x00\x00\x00h\x00\x00\x00 \x00\x00\x00e\x00\x00\x00n\x00\x00\x00c\x00\x00\x00o\x00\x00\x00d\x00\x00\x00i\x00\x00\x00n\x00\x00\x00g\x00\x00\x00'

Now the question is, then what we need to do, to see the content in original form. So that, we or user becomes able to read the content of the file, whatever the content is in. Then we need to decode the data before printing, using the following code:

Therefore, to print normal text or content from the file. Here is the program you need:

fp = open("codescracker.txt", "r", encoding="utf-16")
print("\n------The content is------")
print(fp.read())
fp.close()

produces:

------The content is------
New content with encoding

Note - The file encoded in one encoding format say utf-16 can't be encoded or decoded using other encoding format say utf-8. If you do so, as done in the program given below, it will produce error. Let's see how:

fp = open("codescracker.txt", "r", encoding="utf-8")
print("\n------The content is------")
print(fp.read())
fp.close()

The above program will produce error as shown in the output given below:

python encoding decoding example

Note - The file codes.py available in the directory:

C:\Users\DEV\AppData\Local\Programs\Python\Python39\Lib

refers to the Python Codec Registry, API and helpers. In your case, in the place of DEV, there may be another name, based on your system's user name. If you open the file, it looks like:

python codes dot py file

Note - But you can re-code or re-encode the data in your desired encoding format, then read the data using that format.

Important - I recommend you to use only UTF-8 encoding format, if you want to encode the data.

errors Parameter in open() Function

Since the errors parameter in print() is used to specify how the encoding and decoding errors to be handled. The default value is None. That is, if you'll not use this parameter, then it is considered as None, if you'll use this parameter with value None, then it is also considered as default one.

Here are the list of values, that can be given to errors parameter in open() function:

Note - Basically, the errors parameter in open(), used to read the file without raising the exceptions. This can be done, only by providing the values to errors except None and 'strict', as both these are almost works as similar.

Here is the simplest example program, uses errors parameter, with 'ignore' as value to read the file:

fp = open("codescracker.txt", "r", encoding='utf-16', errors='ignore')
print(fp.read())
fp.close()

Here is the output, you'll see:

New content with encoding

Since, the file previously, written after encoding the data using utf-16, therefore I've used the same encoding format to read the data from the file. The question is, what if we read the data from the same file, using other encoding format say utf-8, without using errors parameter. Let's find out the answer. Here is the program:

fp = open("codescracker.txt", "r", encoding='utf-8')
print(fp.read())
fp.close()

If you execute the above program, here is the error message you'll see on output screen:

python errors parameter in open

As you can see, the UnicodeDecodeError error is produced. Therefore to open the file and read the data, without raising the exception, we need to use the errors parameter, with 'ignore' as its value. Here is the modified version of previous program:

fp = open("codescracker.txt", "r", encoding='utf-8', errors='ignore')
print(fp.read())
fp.close()

Now the output produced by above program, will be:

python open function errors parameter

Note - I do not recommend you to do the same, or to bypass or to use errors parameter with 'ignore' value, while writing the data. It may lead to the data loss.

From the above output, still the output is not that we want to see. That is, we're able to ignore or remove the error message, but the output is not in the readable format. This is because, the content in the file, was encoded using utf-16, but we're trying to read it, using utf-8.

newline Parameter in open()

The newline parameter can be used to define the work of newline. The value of this parameter can be:

These values to newline parameter, only applies to textual data. If you do not use this parameter, or use it with its value None or '', then the universal newlines mode is used.

Basically, newline parameter is used to provide the escape sequence such as '\n', '\r\n', '\r', for ending the line. And all these characters are translated into '\n' before returning to the caller.

In most of the cases, by system's default, the escape sequence '\r\n' gets automatically added at the end of each lines.

Note - As you can see in the output given in rb mode Example section, there, we've seen that the output includes \r\n in the place of end of line or newline, when we opened the file in binary reading mode. But if, you use newline parameter with \r or \n while writing the content to the file, from where we read the data, then exactly this or \r or \n will gets seen in place of default \r\n.

closefd Parameter in open()

The closefd parameter can also be known with or stands for close file descriptor, used to wrap a file descriptor representing an already opened file, or to open a given file from its name.

Note - The file gets closed when the returned Input/Output (I/O) object is closed, if the closefd parameter is used with True as its value.

Note - The closefd parameter is a Bool, it takes either True or False as value.

If the parameter closefd is assigned with False, the underlying file descriptor will be kept open when the file is closed. This doesn't work when the file name is given. Therefore it must be assigned with True. Since the True is the default value, therefore you also remove this parameter to achieve this goal.

Note - A file descriptor is an integer assigned to the file object say fp by the Operating system (OS), so that Python can request the Input/Output operations. To get that integer value of any file object in python, use the following code. This program uses fileno() method to get that inter value:

print("Enter the Name of File: ", end="")
filename = input()
fp = open(filename, "r", closefd=True)
print("\nFile Descriptor Integer Value:", fp.fileno())
fp.close()

Here is its sample run:

python closefd parameter in open

opener Parameter in open()

The opener parameter was added in the Python version 3.3
That is, before this version, this parameter was not used in open() function. Even after its addition, this parameter is almost not used by the Python programmer.

As Said by Officials - The Python official said about opener parameter of open() is, a custom opener can be used by passing a callable as opener. Also the official, not given any particular benefit of using this parameter. May be, in later version, there will be some modification that may play some beneficial usage of it.

Syntax to use custom opener is:

open('codescracker.txt', 'w', opener=opener)

Note - The default value of this parameter is None. Therefore, if you omit this parameter, or if you assign None to this parameter, then it is treated as the default work.

Python Online Test


« Python Tutorial Python Examples »



© Copyright 2021. All Rights Reserved.

CodesCracker