From the course: Python Essential Training

Opening, reading, and writing - Python Tutorial

From the course: Python Essential Training

Opening, reading, and writing

- Watching output being printed to the screen is all well and great, but often as programmers we're asked to produce something real, you know, something tangible, usually for those management types that want to see a physical file they can attach to email or open in Excel or whatever it is they do with files. Sometimes they give you files and they want your program to read them. Fine, we'll use their dang files. Let's go to the code. The first thing you need to know about working with files in Python is that it's not quite as simple as when you double-click an icon on your desktop. We're working a little more directly with the operating system and so there are some things that you need to manage. And one of those things is whether we're simply reading the contents of the file or whether we intend to make changes to it, writing it. And this is because it causes problems if two applications are making changes to the same file at the same time. So the operating system needs to know who's doing what. So I can use the open function and pass in the name of a file. And I have a file 10_01_file.txt right there. So I'm going to write 10_01_file.txt. And then the second argument is going to be the string R. We're going to open this file in the read mode and if we print f at this point, we get a file object. And there are a couple ways to get the actual text inside the file. And the first is readline, so f.readline. And this reads the lines of the file one at a time. You can see if I run it again, I get a different line each time. So this file object contains a sort of bookmark of which lines of the file it's already read. So you could obviously put f.readline inside of some sort of a loop and get all the lines of the file. But there's a second easier way if you want to get all the lines of the file, and that is readlines, plural. And this gets all of the lines of the file that haven't been read already and then puts them into a list of strings. We can print out the contents of this file fairly nicely if we do something like this, for line in f.readlines, print line. Notice that these lines are all double-spaced and that's because each line of the file has a new line character on it on the end there, and this print statement also includes its own new line. We can fix this by stripping out any leading or trailing white space, including new lines, and we do that with the strip function on each line. Beautiful, and that is better than ugly. Now let's look at writing files. So we're going to do something similar to this but instead of an R, we're going to do a W for write. We're also going to call this output.txt, which doesn't exist yet, but when we run this, it'll create the file for us which is a pretty nice feature. So if we go over here, okay, we see that file's created and then let's write a couple of lines. So we're going to use the write function, write line, line 1, f.write, line 2, and then run that. And if we open this output file over here, hmm, there's nothing there. So what's going on? Well, writing to files is an expensive operation and Python tries to make file writing more efficient by putting all of the data you're writing to the file in a buffer. And it only writes to the file when that buffer gets full or when you close the file. So if we close the file, we can do that with f.close and then run that. Let's close this file and then reopen it. There's our lines. So what's going on here? You see it didn't print a new line character between the lines. We have to add that ourselves when we're writing files. So let's add that new line character, open this file again, write those lines, and then close it. Let's reopen the file. Okay, now you see our two lines, but what happened to our previous data in this file where they were side-by-side like that? Well, when you open a file in write mode and start writing to it, it's actually more like a create mode. Python will overwrite any existing data in that file and we can fix that by opening the file in append mode instead. So we're going to open this file again, but instead of a W, we're going to have an A for append. Then we're going to take these and write line 3 and line 4 and then close the file. If we open this file again, you see we get all of our lines. And to close this lesson, let's talk about this close line. It's very important that this gets called. It basically releases the file, telling the operating system we're all done writing to it. Now these files will eventually get closed. There's a process in Python that cleans up old variables that aren't being used anymore and will close them. That's kind of a longer discussion. Unfortunately, that behavior is unpredictable and you might get some kind of strange behavior if you're not managing the closing of these files yourself. It's best practice to close them when you're done with them. And the most common way to do that is with the with statement. So let's say with, open, we're going to open this in append mode as f, f.write, some stuff, f.write, some other stuff. Okay, and as soon as we outdent and leave this code block, this file gets closed. Notice that we still have access to this variable f, though. So if I run this, it will write that and f still exists. So it's not like the if statements or functions where you only have access to if inside of that block. However, if I tried to do something like this, f.write, PS, I forgot some stuff, we get an I/O error or an input/output error because this file is already closed for writing. So file this away in your brain because we're going to be reading and writing, although no arithmetic, throughout the rest of this chapter. Ugh, more of these.

Contents