File Handling in Python


If you look at the background of your desktop, what do you notice? Files spread everywhere, right? I know we regularly fall into this bad habit of having a cluttered desktop, but the point here is that we seem to deal with files a lot.
Talking about Python, it is certainly possible to work with files using Python, and this is what I will be showing you in this tutorial.
The first natural process we have to perform in order to do any operation on some file is to open it. Go ahead and download Text.txt, which is the file we will be using to conduct our experiments.
In order to open the file using Python, we will use the open() function. As mentioned in the documentation:
open( ) returns a file object, and is most commonly used with two arguments: open(filename, mode)
Before taking an example, as mentioned above, the open() function will take two arguments: filename andmode. This way of setting arguments is actually similar to stdio's (i.e. C) fopen()Filename is simply the name of the file you want to open. You can also provide the path to that file if it isn't in your current directory. And mode is a string value that indicates how the file is going to be opened. Examples of different modes are shown in the following table:
ModeDescription
rOpen text file for reading. The stream is positioned at the beginning of the file
r+Open for reading and writing. The stream is positioned at the beginning of the file
wTruncate to zero length or create text file for writing. The stream is positioned at the beginning of the file
w+Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file
aOpen for writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the current end of file
a+Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file. Subsequent writes to the file will always end up at the current end of file
You can also see other modes in the Python documentation.
Let's go ahead and open the file Text.txt. This can be simply done in one line as follows:
text_file = open('Text.txt')
As you can see here, we didn't specify the mode. To know the default mode used with open(), type print text_file, in which case you will get the following:
<open file 'Text.txt', mode 'r' at 0x10728c810>
So, as you can see, the default mode is r. At this point you may not notice anything happening, but opening the file has been achieved in this step.
The file is like a secret box. We have opened it in the previous step, and now we want to see what's inside. Reading a file is simple in Python and can be achieved using the read() method, as follows:
text_file.read()
In order to see the output of the previous statement, type print text_file.read(). In this case, the secret (content) will be revealed!
I'm enjoying learning about "file handling" in Python
Go ahead, and type print text_file.read() again, and what do you notice? You can see that an emptystring has been output. This is because you have already read the whole file, in contrast to reading one line at a time, i.e. readline().
Maybe you'd like to add (write) another sentence or paragraph to the file we have already read. Say you wanted to add the following sentence: and I'm looking for more. This can be done in Python using thewrite() method, as follows:
text_file.write('and I\'m looking for more')
Oops, we got an error here:
Did you get the point yet? Remember when we opened the file in the first section as follows: open('Text.txt')? Since we didn't specify a mode in that example, the mode (default) was set tor. This will allow us only to read the file, and in this example we are trying to write to the file. So, let's open the file again, but with the write w mode:
text_file = open('Text.txt', 'w')
Try now to write the sentence again using the code above. What did you get? Let's open() and printread() the file as shown in the first two sections to see what's inside the file:
The output of this script is:
and I'm looking for more
You were expecting to see this new sentence accompanied with the original sentence I'm enjoying learning about "file handling" in Python, weren't you? So where did it disappear to? The w mode seems to overwrite the content of the file it is writing to. If you are interested in keeping the original text of the document, you can use the a (append) mode:
In this case, you should get the expected content in Text.txt:
I'm enjoying learning about "file handling" in Python and I'm looking for more
Let's put everything we've learned so far together:
We have some new things here. Let me clarify them quickly. with...as allows us to open the file, process it, and make sure it is closed. seek(), on the other hand, allows us to move the pointer (i.e. cursor) to some other part of the file. seek(0), for instance, means that the reference point will be the start of the file.
Go ahead and run the script, and see what you get as output.
Having the habit of closing a file after reading or writing will enable you to free memory. Yes, Python will automatically close the files after the script finishes, but if you don't do that beforehand, any open files will occupy some space Python could benefit from. Closing a file is simply carried out using the close()method.
Let's take this example:
What will happen if we close the file after read()? That is, text_file.close(). Python will give the following exception:
Thus, we conclude that we cannot read from or write to a closed file.
That's it for this tutorial. We have seen how easy Python makes it working with files through some basic operations we have demonstrated. 
What other operations do you usually perform on files using Python?[full_width]