How to Open a File in Python : Mode With Examples

How to Open a File in Python _ Mode With Examples

Python is a popular programming language that allows you to develop web apps, and perform data analysis. Do you want to know how to open a file in Python? If yes then you come to the right place. In this post, you will learn how to open a file in Python in different ways.

Modes available in Python

Before going to the coding parts let me know all the modes available in Python.

“r”: Read Mode. It allows you to open files for reading and it is the default mode.

“w”: Write mode. The file is opened for writing in this mode. And in case the file does not exist in the path then a new file will be created and if it exists then its content will be erased.

“a”: Append mode. It allows you to open files for writing but here the data will be appended to the end of the existing file without overwriting the existing data.

“x”: Exclusive Creation mode. This mode is used for writing only when a file doesn’t exist. If the file is already in your path then an error will occur.

“b”: Binary mode. Here the file is open in binary mode. The binary mode is used for images, audio, or video files rather than text files.

Examples to Open a file in Python

In this section, you will know all the examples to open a file in Python. Let’s start

Example 1: Open a file in Python in read mode

You can open a file in read mode using the open() function. Inside the open() you will pass the filename and mode as arguments. Run the below lines of code to read the text file from the current directory in read mode.

file = open("example.txt", "r")
content =

Here you will first create a file stream using the open() function and then read the content of the file using the read() function. After that, you can display the content of the file.

Example 2: Open a file in write mode

You can open a file in write mode by passing the “w” mode in the open() function. Let’s say I want to create a file with the name “sample.txt” with some text content then I will open the file in write mode.

Use the below lines of code to open and write some text to the file.

file = open("sample.txt", "w")
file.write("Welcome to the Data Science Learner")

Please note that if the file already exists in the directory then its content will be erased and write the new content in it.

Example 3: Opening a file in append mode

In case you do not want to overwrite the existing content then you will open the file with the “a” append mode. Here you will pass the filename with the append mode to append some new text to the existing text file.

Run the below lines of code.

file = open("sample.txt", "a")
file.write("You will get tutorials on data science")

After doing all the operations you have to also close the file. It ensures that the allocated resources for the file have been released. To close the file you will use the close() function.

Add the below line to close the file.



There are many modes to open the file. In this tutorial, you understood how to open files in Python in reading, writing, and appending mode. At each operation, you must close the file using the close() function.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner