In any Programming Languages, file handling is the one of the most important topics. While working for big companies and websites, we often use file handling, sometimes to automate things, make logs, save data etc. If you are familiar with Python Programming Basics like installing Python, working with IDLE and other 3rd party Libraries of Python, I know for sure you’ll like this. First, let me tell you what exactly is File Handling.
The File Handling is the aspect of Programming which includes system files. As for any Operating system, the File Handling is the core of the system where one can perform tasks like creating, modifying and deleting the system files. I will not give you a hard to understand definition of file handling, it’s just this. In other Programming languages like C and C++, the File handling was a bit lengthy and complex. But File Handling in Python gives you more power over the files with simple functionality. So, let us not waste time and begin with the File Handling in Python.
File Handling in Python
For this tutorial, I am going to use IDLE from Python 2.7. If you are new to this please read How to Install Python. There are two aspects of file handling. Either you write something into the file or you read the file. We’ll do the both. First let us code to write something into the file. Start your IDLE and put these lines of code –
def main(): f = open("textfile.txt","w+") for i in range(10): f.write("This is line %d\r\n" % (i+1)) f.close() if __name__ == "__main__": main()
Let me explain what I did here. First we have to make a variable which calls a function name “open” for opening a file. The open() function have two arguments, the first one is the name of the file and the second one, the operation you want to perform on that file. In the above code, the file name is “textfile.txt” and the operation string is “w+”. The “w” in means that we are performing a write operation and the “+” sign means to create the file if the file doesn’t exist. It’s simple as that. Now, we have opened a file and let’s write some data into it.
Now we using a for loop to put some data into it. We are calling write method which writes data into a file. By doing this, we will insert 10 lines into the file. Now, the file has some data and we need to close the file. To do so, we’ll use the close() method. Go ahead and save the file and run it. You’ll see the directory contain 1 more file named “textfile.txt” with 10 lines in it. That’s it. But sometimes, we want to add data into the file without losing data which is already present. The permission “w+” will delete the previous data of the file and add the new one every time we call it. For this we have another permission or operation in file handling in python, appending.
Appending simply adds data to the file after the last line of the previous one. To open a file to append, you need to do is change the permission with “a+”.
/.... f = open("textfile.txt","a+") ..../
Now if you again run the program, the file will contain 20 lines. So for file handling in python for beginners, this must be enough for the writing part. Now let us move onto the reading part. In this article, I will teach you how to read a file line by line and entire file at a time.
Now enter the following line of code and comment the writing part of the code –
def main(): f = open("textfile.txt","r") if f.mode == 'r': contents = f.read() print contents if __name__ == "__main__": main()
We have opened the file with mode or operation “r” which is read. The if condition checks if the mode is “r”. After it we use the read() function to read the entire file at a time and put that all in the “content” variable. To print the data of the file, we will print the “content” variable.
If we want to read the file line by line we can use the method, readlines(). Check the code below –
def main(): f = open("textfile.txt","r") fl = f.readlines() for x in fl: print x if __name__ == "__main__": main()
We use the readlines() function to read one line at a time and print the entire file data using a for loop for x number of lines in the file. In this way, you can read the data of the file.
So, this is all file handling in python a beginners should know. For latest Tips and tricks, follow SandwichBite.com If you have any question or query, please comment below. We are happy to help you. Till then, Happy Coding.