We have been working with variables in the previous tutorials and we store some data in them but the data is only temporary. What if we want to store data that is permanent? Python has the ability to work with externals files.
In this article, we’ll learn file handling in python like how to open a file in python, read a file in python, write to a file, and then closing a file because closing a file is necessary.
How to open a file in Python?
We use a built-in function known as
open(). This function accepts two parameters, filename and mode both separated by a comma where mode is the operation that you want to perform on the file like read
r or write
Let’s understand this using an example. Suppose, we have a text file named “example.txt” containing the following lines.
My name is John
I am 32 years old
I love to play Cricket
My favorite color is Blue
Yo, open this file or open this file in read mode, we’ll type the following.
f = open("example.txt") or f = open("example.txt', 'r')
By default, python always opens the file in read mode but we can specify which mode we want to open the file in.
There are 6 different modes that you can open a file with.
- Read “r” – This is the default mode
- Append “a” – Opens file for appending, creates a new file if it doesn’t exist
- Write “w” – Opens a file for writing, creates a new file if it doesn’t exist
- Create “x” – Creates a file with the given name
- Text “t” – Opens the file in text mode
- Binary “b” – Opens the file in Binary mode
How to read a file in Python?
We know how to open a file in reading mode using
r. To read a file, we must make sure that we open the file in this read mode and to read the file, we use
read() method. Let’s use the same file that we created above.
f = open('example.txt', 'r') print(f.read())
My name is John I am 32 years old I love to play Cricket My favorite color is Blue
Now, when we have printed the lines by using
f.read(), if we use
f.read() again, it will not print anything because the cursor is now at the end of the file. To change the cursor back to the beginning, we type this.
To print only first line, we will use
print(f.readline()) # if you type readline again, cursor is now at 2nd line print(f.readline())
My name is John I am 32 years old
You can also define the size of the data to print inside the brackets of read method. It will be like
If you thought it will print line number 4, you were wrong. It will print number of characters mentioned in size.
How to write to a file in Python?
To write to file in python, first we use
w keyword while opening a file. With this mode, if the file is not present with that name, it will create a new file. Now, when the file is opened, we use
write() to write anything to that file.
wr = open('test.txt', 'w') wr.write("Test")
Now when you check the file, you will see Test written inside it. The problem with write mode is that now we have the file with test and if again write to the this file, the data will over write with the new data written.
To avoid this, we need to open the file in append
test = open('test.txt', 'a') test.write("New Test Statement")
Now, if we view the file, it should have both the statements, “Test” and “New Test Statement”.
Closing a file in Python
We have learned to read and write data to files. At this stage, the file is using resources and we need to free up these resources by closing the files.
We do this by using the
f = open('example.txt', 'r') f.close()