Python Institute PCAP – File IO and Exception Handling in Python
- Exception Handling
And it does exactly what it sounds like. Let’s say you have a user using your application or website or whatever, and they perform some kind of task and all of a sudden there’s some kind of error. Okay? We’d like our application to ideally handle that error in a graceful manner, right? You don’t want the user of your application to get, see, a scary message that exposes the internal code of your application. That’s really bad, right? Poorly designed websites go through this where if there’s an error that occurs, the internals get exposed to the user and they see this really long stack trace, almost like a log of all the errors that took place. And that’s no good. You want to handle your errors gracefully. And errors or exceptions are a common thing in application development. All applications run through this. So we need to be able to handle these in a more graceful manner so that we don’t scare the user away. Okay, so what do I mean by that? Well, I have some code that’s pasted here, let me paste that code. And you should be familiar with what this is doing.
But I’ll just give you a quick summary. We’re defining a function here, sum, and it accepts two numbers and we’re printing the sum of those two numbers, okay? Now, here’s the thing. Notice number one, we have this variable and that accepts input. Remember this input function? This takes input from the user of the application in the command line in the console. When we enter any number or word or whatever, that’s what you use for input. That data that the user enters gets captured in this number one variable. That’s what I titled it. And so now I’m passing in the number twelve as well as number one to the sum function. What’s going to happen? Well, if you recall, the input function actually accepts everything as a string. So even if you enter a number, it’s going to convert that number into a string representation and save it into the number one.
So when we use this function here, we’re actually not using it correctly or the intended way. We’re passing in a string and we’re passing in a number. And you should know by now what happens when you try to add a number to a string. We should get a type error. And I’ve shown you this before, right? So let’s run this and see what exception occurs. Let’s run it. It’s saying enter a number. So let’s enter the number ten and boom, we get an error. Notice type error can only concatenate string, not int to string, okay? And as you can see, a process finished with exit code one. So anytime the exit code is anything other than zero, that means there’s an error that happens, there’s an exception, right? So this is the error, okay? Or the exception, same thing.
So rather than displaying this to the user of the application where it tells you, hey, in this file, at this line number, this particular function exists and it did this. You don’t want that. You want to handle this in a more graceful manner. So how could we do that? So in Python and all programming languages have the ability to do exception handling, there’s a specific syntax that you use and that is known as a Try Accept syntax. So the way it works is we try to use the term use the keyword try here and this is a code block. And after try there is Accept. You put a colon and then the code that you type here will run. If there’s an error in this Try block, I’ll explain it to you.
Let me type, there was an error, okay? And that’s it. Let’s just say that that’s the only thing we want the user to be able to see. We don’t want the user to see this nasty message here that exposes the internals of our application. So now if we run this again, this is what the method or function is supposed to do, right? In the try block is are the instructions of what the function is supposed to do, right? But if there’s an error anywhere in this Try block, then this happens, which is the Accept section. So we know that there’s going to be an error, but we also know that now it’s going to be handled gracefully. Let’s run this application, enter the number, let’s just enter ten again, hit enter and boom. There you go. Notice it says there was an error. And so when that happens, we can move on to bigger and better things in the code, right? We can have a very large set of instructions here, really a very massive program, but it won’t crash because we’re handling the exception.
When you actually don’t handle an exception and a crash occurs, your program ends. That’s it, it’s over. But with this Try Accept block, you can actually handle exceptions and prevent your program from completely crashing. Now, there are some practical software design principles that you should follow when you use exception handling. Exception handling or error handling should be used only when there’s an error that happens that you really have no control over, that could happen, right, that you won’t expect. For example, let’s say your software relies on the Internet and goes and downloads files from the Internet. For example, right? Now if a user is running your program but they run it with no Internet access, that could cause a crash.
Now of course, if you’re a good software developer, you’ll code for that type of scenario and you’re not going to throw a dirty error on the screen showing that this file was attempted to download from this such and such website. So in that kind of a situation, you should be using exception handling. Another scenario is for example, your program relies on a file to exist on the file system. It reads some kind of file with a given name or whatever and let’s say that that file for some reason got deleted. And when the user tries to run the program, the program of course is going to look for that file and if it’s not there, it’s going to crash. If you don’t have exception handling in place, okay? So for situations like that where it’s environment dependent situations, that could happen but you don’t expect them to happen. You want to make sure you use exception handling to COVID those types of scenarios. Now in this simple scenario we don’t really need a try and accept section because for example, let’s say the user enters number one and number two and one of them is a string.
We can check for the data type of each of these parameters that are passed to the sum function. And if any of them are not a number, we can just print to the user and say, hey, listen, you need to be giving only numbers to this function, okay, rather than letting it crash. And for that type of scenario you do not need a try and accept. For that you can just have an if statement to check the data type of each of these and if they suffice then the function will run correctly. If they don’t suffice, you can have an else clause that says hey, enter the proper data type for this function. Try accept is used for situations that are kind of out of your control and forcing the data types of NUM one and number two to be in is certainly totally in our control.
So we don’t need exception handling here, okay? So we can get rid of this and use a different kind of way in which we can make sure that the function is used properly. So you can probably already guess that we’re going to need to check for the data types. For each of the parameters there is a function known as the type function which we’ve already looked at for sure. There’s another one that could be used in Ifelf’s statements and that is known as is instance. So let me use that one. So if we do if is instance number one is of data type int and is instance, we do the same thing for the other parameter, NUM two. And we say if that is of type int, then we’re going to print number one plus NUM two. Else we can notify the user and say, hey, data type was not a number for the parameters and this should work perfectly fine. And notice we were able to handle this issue without using exception handling. And so when you can do that, make sure you handle the issue without exception handling. Don’t just put exception handling everywhere. Things that are in your control, you should certainly write the code and the logic to handle those kinds of scenarios like we’re doing right here. We did not need exception handling in this situation.
There’s an important point I’m going to repeat myself. Things that are out of your control, only for that you use exception handling. Things like not being able to connect to the Internet while your application is running, the disk running out of space on the machine, or a file getting too large for your program to handle. Things like that. Those cases for that, you should have exception handling in place. And what is exception handling? That’s the try accept block that we looked at, right? You try a piece of code, if there’s some kind of error that pops up, then it jumps to the accept block and it does what the accept block is doing, right? So we’re going to look at a practical scenario in the next lesson where we actually try to open a file. And I’m going to show you how to read and write file files. And we’re going to explore the scenarios in which the file gets deleted from the system. And so we’re going to handle, for those types of exceptions.
- File IO
That basically has to do with reading and writing files. And this is a very important topic and this is going to build on a bunch of different topics. And I’m also going to involve exception handling, which we looked at in the previous lesson. We’re going to build upon these ideas of dealing with resources in your computer, such as files, and what is involved with opening files and reading files and closing files and that sort of thing, and where exception handling comes into play. So let’s get started. The first thing what I want you to do is download a file that I’ve provided as part of this lecture. If you go to the resources section or underneath the video, depending on what platform you’re using, you should see a file that’s attached to this particular lecture. Download that file, and I’ve done that right here on my desktop, my Mac machine. Here on my desktop. I’ve got this file right here, sample text, okay? That’s the file name. And as you can see, let me just open this up. It’s just a bunch of some sentences and this has to do with Ketosis and so on, but not important to go over what it says, that’s just some text that I borrowed from the Internet. So we’re going to experiment with reading and writing things to this file. Okay? So let’s start with how to open a file.
And the way to do that is use the Open function. And as an argument to this function, we give the location for where that file resides on your computer. So in my situation, let me just open up the terminal here. Let’s clear the screen. So in the terminal, I’m on my desktop, right? So if I type in PWD, it tells me where I am on this machine and it’s in the desktop. That file is located on my desktop. So I’m just going to copy this exact location and paste it inside of a string like that and then do another slash and then give a file name, which is the sample. And as soon as you type in S, you can see the IntelliSense is smart enough to go to that location on the desktop and pick up the different files that I may have. And one of them is Sample text. That’s the file that I downloaded. So let’s have that file there and this is going to open that file and I can save this in a Myfile object, my file variable, which is going to be a file object.
And using this My File object, I can read the contents of that file and it’s as simple as just doing myfile dot read, okay? And I can once once you read it, you can save the contents of that file in a variable. So let’s just call that variable content. Okay? So once this line is invoked, the file is read from beginning to end and all of that data that’s in that file is captured in this variable called content. And now I can print, for example, the content. Let me just go back. I think I open a different file. Now I could just print the contents of the variable content, right? So let’s just do print and just type in content. All right. So let’s run this, and there you go. We see all the sentences being printed out here. Okay. Now here’s an interesting thing. If I run this command again right after this one, so we read the contents of the file again and we save it into the content variable again. First of all, you’re going to notice there’s highlighting going on here. It’s saying declared content defined above without usage. So it’s saying that this particular content variable is not being used. So let’s save it into some other variables such as data. All right? And so now if I print the contents of data, let’s do that. So now I’m just printing out data here. So let’s run this and notice nothing gets printed.
What happened here? Now if I print content, let’s run this, something does get printed. So this line is reading perfectly fine, but this line is not reading properly. What’s going on here? Well, it doesn’t have to do with what you name the variables, obviously. Hopefully you figured that out by now. It does not have to do with what you name the variables. It has to do with how this read function or method rather works on a file object. What happens is, when this read method is invoked, it takes the cursor from the beginning of the file all the way to the end of the file, and that’s it. It stops right there. Okay? It doesn’t go back. So when you try to read the file again, it’s not actually going to read anything. It’s already at the cursor is already at the end of the file, so nothing really gets read. That’s why the data variable here was empty. So when we tried to print it, it didn’t give anything back. It didn’t read anything. So to be able to reset a cursor, we used the seek method.
Okay? So let me just copy this thing, cut it out, and paste it right underneath content. And so after this line is printed, it’s going to run perfectly fine. It’s going to print the file contents. But when this line runs, data is not going to have anything. So let’s fix the cursor. And the way we can do that is we can say myfile seek and you go to zero, which is going to be the notice. It says the offset index position. So it takes us to the first, basically, letter of the file, right? And the beginning of the file is where it starts from again. So this function or method rather takes the cursor all the way back to the beginning of the file. If you don’t use this method right here seek, then it will not be able to read properly. All right? And so we take the cursor back and now we should be able to print the data as well. So let’s print that. And so now, just to separate the two things, what I’m going to do is I’m going to just print something here so we can differentiate between the different print line statements. I’m just going to put a bunch of dashes here so you can see that this print is working as well as this print line is working, all right?
So let’s run this, save the file, let’s run it and boom, there you go. Notice the first section printed properly. We’ve got the divider here, which is right there. And then we also continue to print for the data variable as well. It works exactly the same. This was the key thing that resetted the cursor, okay? Very important to keep in mind. Now this basically takes all the strings or numbers or whatever it is. It just takes it as a string from the file and dumps it into this variable called content. That’s what read does. It takes the file literally, the way it’s format and everything, and dumps it into whatever variable you assign, such as data or content. But if you want to read these lines separately into, for example, a list, what you could do is each line could be an element in the list and there’s a special function for that, or method rather. I keep saying function.
Remember? What’s the difference between a function and a method? A method is a function that’s defined as part of a class, okay? It belongs to the object such as this read. This is a method that belongs to the My file object, right? A function doesn’t belong to anything. For example, print is a function. It doesn’t belong to anything. Same thing with open. It’s just a function. It doesn’t belong to an object. So similar to the read method here, we’ve got something called the read lines method, which will allow us to take the contents of the file and save them in a list, okay? And each line of the file is going to be saved as a separate element in the list. So let’s experiment with that. I’m just going to cut this out so that we don’t have too much things, too many things printed to the screen, all right? And down here is where I’m going to create that list. So what I’m going to do is I’m going to say content list and we’ll say My file dot instead of the read method here.
Notice we got a couple of other options. We’ve got read line and we’ve got read lines. Let’s choose read lines. And this is now going to save each and every single line as a separate element in a list. And that list is going to be this content list variable, okay? And so now, if I was to print Content list I want you to pay attention to what happens here. You’ll notice that this doesn’t work. And I want you to hopefully be able to figure out by this point why it’s not going to work. Let’s run this. Notice. It prints an empty list. Why is that? Because oh, we didn’t do the Seek After this read, did we? So we need to use this again. Down here. Now. It should work. So let’s run this.
And there we go. Notice this is the list. And each line is basically an element in the list. So let’s see how far this line goes. Yep, it Goes all the way up to this point. Notice there’s a new line escape character which is back end. And then we have an empty line, which there’s no data in that line. Let me just show you real quick. Let’s open up the file so you can see in sample data. Notice after this particular line is over. We’ve got an empty line here. So that empty line goes into its own element in the list. And then we have this next line, which is another long sentence which goes up to this point. And then finally we have another line here which also goes up to this point. And then we have another empty line, which is a separate element of the list. Okay, so that’s what Read line does. It takes each line in the file and puts it into a separate element of the list.
Now, it’s very important when you’re opening and reading things from a file like this, in this, using this kind of format. And I’ll show you a different format to do this, which is a bit more succinct. When you’re reading contents of a file like this, you’re opening the file. You also want to make sure that you close the file. Because let’s say you have this program and you opened it. You opened the file and you did things with it in some later part of the code, in some other class or in some other module that some code is going to need to open this file again. Right? If it tries to open it, it’s going to give an error. It’s going to say? Hey, this file is already opened. So you can’t have two threads doing things with the same file at the same time. Not In Python like this, not like this. So whenever you open a file, you also want to make sure you close it. And the way to do that is this file object called my file. We actually close it. So let’s do that. We can close it right before we print.
And we can say myfile close. All Right. And now, if you hit run, everything will work as it should, and you won’t run into an issue where later on in this code somewhere we tried to write something to the file that’s already opened, and we’re not going to run into an error. We safely close that file. We close that resource. This is one way of opening a file, reading the contents of the file and then closing it. Another way is to use the with clause or with keyword. Let me show you how that works. So let’s go down here. That goes like this. We type in with and then you use the Open clause like we were and you pass in the file name. So the file name is, of course, in this particular location. And again, if you’re using Windows, you want to make sure you use backslashes, two backslashes. So again, I’m going to show you, I think I showed you a graphic in the previous example. But if you do C, that’s how you would need to separate out the directory location and then the file, right? You’d need to change this, change these to double backslashes. Why double back slashes? Because in regular expressions, this could be treated as, for example, an escape character like new line.
Let’s say that my name was nm t I AZ. Now this is going to be interpreted if we didn’t have another slash, this is going to be interpreted as a new line. And we don’t want that. So we need two slashes right there. I’ll correct my name right there. And make sure that in Windows, this is the format that you use. Since we’re not using Windows, we’re just going to use the forward slash and reduce slash like this. Just one forward slash separating each of the directories and then the file like that. So with Open, we use as and we define a particular variable. This is the file variable that we’re going to use. So we’ll say my file and then we can use my file object to read and we can save that into a content variable like that.
Okay? And then of course, we’ve got content populated. Again, let me change this to some other variable. We’ll save this to new content. And I don’t think we have a variable up here called new content, right? And notice it’s giving well, let’s try to print this and you’ll see what happens. Let’s print new content like that. Let’s save it and let’s run it and boom. There we go. It’s working as expected. This is the read lines that are being printed earlier from the code that’s above. And this last part is just reading using the read function or method rather. And this could be changed to read lines or any of the other file reading of methods that we have access to.
And you can look at a couple more. Now, this Open function has another argument that it can take, all right? And that is called the mode, the mode in which you intend to open this file. You can open it for the purposes of writing to the file. You can open it for the purposes of reading to the file. Reading from the file. You can also open it for the purposes of just appending to the end of the file, right? And you can also open it to be able to override it. And the way to do that is just using W. So let’s go explore some of these choices. So we do mode is equal to, for example, let’s do A, okay? Now, with A, which means append, what I could do is I could say myfile write, and then I can write a sentence.
This is my sentence. And this will be appended to the end of the file after this line executes, okay? And so let me just get rid of this read above. So I’m going to open the file, take whatever data that’s there, and then I’m going to append to it using this, right? And now with this code being in place, whenever some other code tries to open this file, it’s going to have this new data in it, this extra sentence that I appended because, again, I’m using the append mode. Now, keep in mind that using this with open syntax automatically opens and closes the file, okay? So you don’t have to manually go in and close this file. We don’t have to do myfile close like we did earlier, okay? We don’t have to do this. This block of code opens the file, does whatever it needs to, and then closes the file automatically. So when we move out of it and do bigger and better things, we can, of course, open the file again, read it, and so on. So let’s try that.
Let’s open the file again. And the way to do that, I’m just going to borrow this code. Let’s copy that and let’s paste it here. And we’ll say we’ll change the variable to my appended file. Because up to this point, by this point, this particular file has been appended to this given sentence, all right? And so now I can use this and just print the contents of it by doing read like that. This is right in the print function there. So let’s save this file and run it. And there we go. If you scroll to the right, boom. Notice it says, this is my sentence. This is my sentence. It actually did this twice, because while I was recording this lecture, I ran this right before showing this part. That’s why there’s two sentences. As a matter of fact, if I keep running this, let me run it another couple of times. Let’s go to the end and you’ll see that it’s going to say, this is my sentence. My sentence is my sentence, over and over again, multiple times. Okay? One thing to note is that everything is being just appended to the end of the file on the same line, and this is going to continue to happen. You can keep running this, just going to append to the last line. And it could just be a really massive paragraph which will be considered one line in the file.
So to add a new line to it, what we could do is just use an escape sequence and just do a backslash N like that. And what this is going to do is it’s going to just, well, actually, let’s add this before this sentence, right? Because we want this particular sentence to be printed on the next line after all of the sentences that have been appended to the end of the file. So now we’re going to have this new sentence that is going to create a new line because of this new line escape character. Okay? So let’s run this and boom. There we go. You see, this is my sentence on a new line because we added the escape character for a new line. And then also we still got all those previously appended sentences. Okay? So that’s append mode. Now there’s also write mode.
So if I do this with W now mode is changed to W. Now this is what this is going to do is going to overwrite the file. It’s not going to append to it. W means overwrite. Okay? So now if I just run this line after you’ll see that it gets replaced, the data gets replaced. All the sentences that you see here are going to be replaced by just this one sentence. Okay, let’s run it. I’ll show you what happens. And boom. There we go. This is the code from before. As a matter of fact, let me just comment all of this previous code out. We don’t really need that for now and let’s save it. Now let’s run this again. Notice this is the only thing that gets printed. As a matter of fact, the new line is also here. Okay, the new line is also here as well as this sentence. I’ll show you. Let’s go to the file. Sample text. Let’s open it up. Whoops. I think I already had it opened. Now let’s open it again. There you go. So as you can see, the new line was the top line right there, which is empty. And then we have this sentence right here. Okay, so that’s what write mode does. It overwrite the file.
Now you can actually change the file name to something else and it’ll basically create a new one if you want. So I’ll say sample two. Sample two dot text. Now sample two dot text does not exist on my desktop. As you can see, we’ve only got sample text. We don’t have sample two text. But if you open it with this W mode, with this write mode, it’s actually going to create a new file called sample to text. And it’s going to contain this sentence. Let me run it for you and you’ll see. As a matter of fact, I’ll also change this. Well, let’s just comment out this code.
We’ll go to the desktop and I’ll show you what that file contains after this code is run, okay? Rather than printing it. So let’s run this. So something happened. We get an exit code of zero, meaning everything worked fine, no errors. Nothing gets printed, obviously, because I commented that stuff out. Let’s see if we can find the sample two text. Let’s go to our desktop and lo and behold, this is that file. Sample two text. If you open it up, it’s going to contain the same data that we did for the other file. So pretty cool. Now, if you open a file in read mode like we were, if you just put R here, that would be opening the file in read mode. Okay? If you open it in read mode, you can’t write to it like this.
All right, I’ll prove it to you. Let’s try to run this code. Notice it gives an error. It says, myFall write, not writable. The reason is because the mode was R, it was set to read only mode. If you want to be able to read and write to a file, you can use R plus mode. Okay? R plus mode allows the ability to read from a file as well as write to it. So we’re actually writing to the file right now. So let’s write to sample two text. And I’m going to change this sentence to hello there. Right. So what’s going to happen? Sample two text is going to be replaced with this content right here. Whatever it had before, it’s not going to have it. It’s going to actually replace that content because we are using the right method and we also are using RW, so we’ll be able to read and write from that file. So let’s run it. And so now let’s check out sample two text on my desktop. Let’s open it up.
And there we go. Notice it does have that hello there and the new line that we added. But what is this sentence here? What is this doing here? Well, when you open it in read and write mode, what happens is when you start to write, it writes from the beginning of the file. But whatever is there, it leaves it there. It doesn’t replace it. It doesn’t overwrite the entire file because this is used for reading and writing R plus. If you want to completely replace it, you use W, okay? And that will, of course, replace it. Let’s double check. Let’s run sample two text. I think I had that open from previously. Let’s open it up again. And there we go.
Notice we just see. Hello there. All right, now let me show you another thing. If I change this to R plus again, meaning read and write, and instead of putting a new line here, I’m going to put Yoyo, this is going to go on the first line, and everything else on the file is going to stay the same wherever it is. So let’s run this. And now let’s open the file desktop. Sample two, text and boom. There we go. Notice it has Yoyo there and notice it has Lo. Hello there. So the hello part was replaced with Yoyo and then lo there is still part of the file. Okay, now what if you wanted to overwrite a file completely and have the ability to read it? For that, you use WR plus instead of R plus. You use WR plus like that. And now what’s going to happen is it’s actually going to replace completely all of the contents of the file and we’ll also have the ability to read from it.
And by the way, when I say the ability to read from it, I can actually go ahead and do myfile read in the same block of code here. All right, when we give read permission and write permission, you can use the read and write methods in the same block of code here, inside of this with section. So just keep in mind when you have W plus or R plus, that means we have the ability to read as well as write in the same block of code. If you just use R alone, that means you can only read. If you just use W alone, that means you can only overwrite the file.
Okay? So now with this W plus, that means it’s going to overwrite all of the contents of the existing file with this word called Yoyo. Okay, so you won’t see the hello there anymore or anything, you’ll just see this. All right, so let’s save it. Let’s run this file, this code right there, we ran it. Now let’s go back to the desktop and check out what that file contains. Sample two, text and boom. Lo and behold, we only see Yoyo. Okay, pretty cool. So we covered a lot of ground in this lesson in terms of how to read and write files, how to open files, and there are two ways of opening files. One of them is like the way I showed you earlier, where you just open the file in this manual kind of way. You save that in a file variable, which we titled My file, and then you can read from it, you can write to it, you can have read lines and so on that’s one way.
And make sure that if you use this approach, you have to also close the file like we are doing right here. We need to make sure we close that file if we open it. Okay, this is more of a manual way of doing things. A more recent and better approach is using this with functionality where it automatically opens and closes the file at the end of this execution. Okay, so inside of the with block, you can have all types of lines of code. Here, it could be anything, a bunch of lines of code. Of course, this is gibberish, but I’m just simulating that you can have many lines of code here, writing different kinds of content to the file and reading from it back and forth and so on.
But it needs to be all part of the with section. And when the with section is over and we move on to bigger and better things, by then the file is already closed. As soon as it jumps out of this with open section, the file is already automatically closed. You don’t have to manually close it. Okay? I’m repeating certain concepts because these are really important. And again, to learn anything, you need repetition, repetition, repetition, and you need a lot of practice. So make sure you practice this content. And in the next lecture, we’re going to talk about how to incorporate exception handling, how to deal with a situation where you try to open a file that doesn’t exist in your operating system. Those kinds.