As a language, Python is one of my favorites. One of the main reasons behind it, it’s easy to learn. By the way, the purpose of this blog is not how much I love Python. In this blog, we will be going to know about file handling in Python. Basically, it will be a basic overview.
In this blog, basically, I will show you how to create, read, update, and delete a text file. And of course, we will use Python for this. It will be fun!
Create
To create a file, we will use the global Python function open()
. It mainly takes two parameters. The first parameter is for the file name, and the second one is for mode. And it returns a file object.
One thing to remember, if we give the file name in the first parameter, it will create a new file for us in the current directory if the file does not exist. If the file exists in the current directory, then this function takes the existing file. Simple, right?
The second parameter is for how we want to open the file. It could be w(writing), r(reading), a(append), etc.
Let’s see the code.
file = open('myfile.txt', 'w') file.close()
Don’t forget to close the file if you open it.
If you run this code on your machine, you will see a file (myfile.txt) in your current directory.
You can’t set the mode as read if the file does not exist in your machine.
Write
Now we know how to create a file. But this file is empty. There is nothing or no content in the file. So now we want to write something. For that, we will use the write() method of the file object. It takes a string as a parameter. Let’s see in code.
file = open('myfile.txt', 'w') file.write('Python is awesome!') file.close()
After running this code, we can see the given text in the file.
Reading
Assuming that, we have the file we created earlier. Now we want to read the file content using a Python program. For that, we will use the read() method of the file object. Let’s see the code for more clear understanding.
file = open('myfile.txt', 'r') res = file.read() print(res) # Python is Awesome! file.close()
This time we open the file in reading(r) mode. Otherwise, we will get an error when we want to read the file.
If you run this code, you will see the output in your console.
Note: always check your filename spelling when you put the name in the parameter.
Update
Assume that we have a file in our current working directory. And the file name is myfile.txt, and the file content is ‘Python is Awesome!’. But now, we want to add some text to the file.
If we open the file in writing mode and add some text, then it will replace everything in the file with the given text. This way, we can’t add new content to the existing file.
To do this, we have to open the file in append(a) mode. Then we have to use the write() method of the file object.
Let’s see the code.
file = open('myfile.txt', 'a') file.write('\nAnother content.') file.close()
After running this code, our myfile.txt file output will be:
Python is awesome!
Another content.
Delete
Now we will see how can we delete the file which we created earlier.
To do this, we need a Python module called os. ‘os‘ is a Python built-in module. We all know how to import a module in a Python file (import os
). It’s has a method named remove(). We have to provide the file path as a parameter of this method. Since our file is in our current working directory, we just have to write the name of the file. Let’s see in code.
import os os.remove('myfile.txt')
If we run this code, we will see the file is no longer in the directory.
There are so many options and so many things to do in file handling. I have written about some basic operations and try to give you a basic overview of file handling in Python.
If you are a beginner, I hope it will help you.