From the course: Learning Python

Reading and writing files - Python Tutorial

From the course: Learning Python

Reading and writing files

- [Instructor] So now that we've taken a look at the basics of the Python language, we're going to turn our attention to using the rich library of predefined code that comes with Python in order to build functionality into Python applications. Python provides built-in methods for working with files and directories. You can open files, write data into them, read the data back in and so on and that's what we're going to take a look at in this chapter. So let's start by opening up the files underscore start code in our editor and what we're going to do is work with some basic file operations. Now, I don't need to import any classes to work with files because the functions for working with files are built into the base Python language, so for our first example, let's just write some information to a text file. Now to do this, I'll use the open function. So I'll create a variable named in my file and I'll call it open and then open takes a couple of arguments. I'm going to supply the name of the file I want to work with, so that's going to be text file dot txt, and then I need to specify what's called the open mode. So this is the access that I want to the file. So I'm going to in a string, supply the letter W because I want write access to the file, and then I'll also add the plus sign, which means that Python should create the file if it doesn't already exist. So when the open function returns, the result is a reference to a file object that I'm storing in a variable named my file. Now the file is open at this point, so we can write some data. And I'll do that here in this comment and I'll just create a for-loop, so I'll write for I in range 10 and what I'm going to do is call the my file dot write function, to write this is some texts with a carriage return. So the write function just writes data to the file, which in this case is just going to be a series of 10 lines of texts and then when we're done, we need to close the file, so I'll write my file dot close. All right, so to recap, what we're doing is opening the file for writing, writing some data, and then closing the file, okay? So let's go ahead and save this and let's run it and let's see. All right, there's no errors, that's good and you can see over here in the file browser of vs code that this text file dot txt has shown up, and sure enough, there are the 10 lines of text that we written out. All right, so for the next example, let's try adding some content to an existing file. So what I'm going to do is comment out this line that creates the file, and I'll just copy that and paste it here and in this case, I don't want to use the W because that just creates a new file, it'll overwrite the existing one. What I want to do is specify A, to indicate that I'm going to append data to the file instead of overriding all the existing content. And the rest of the code is the same, which means that 10 new lines will be written to the existing file, so let's just distinguish those by putting in this word new so now we write out this as some new text. Okay, so let's go ahead and save this and let's run it. All right and then let's open the file one more time and now you can see that the 10 new lines have been added to the end of the existing file. Okay, so for the last example, let's read the contents of the file that we've created, and there's a couple of ways to do this. So first, I need to open the file for read access. So what I'm going to do is comment out the previous code that I've written, and I'm going to use the open function again, so I'll paste that in. So in this case, instead of opening for writing or for appending, what I'm going to do is open the file for read access by specifying the mode of R and then to make sure that the file was opened correctly, I'm going to check the files mode, so I'll write if my file dot mode is equal to R that means that the file has been correctly opened and now I can start reading the content. Now for this example, I'll use the read function, so I'm going to write contents equals my file dot read, and the read function will just read the entire contents at once, and then I'll just print them out. All right, so let's go ahead and try that. So we'll save and run, and there's no need to close the file if you're just reading it, right? If you're writing data, you have to close the file, but for read, you don't have to worry about it. So I'll just go ahead and run this, and sure enough you can see that the text is being read in and printed out to the terminals so that's good. You can also read the contents of a file line by line. So if you have a really large file, sometimes it doesn't make sense to read the whole thing at once, especially if all you want is a small piece of the content. So the read lines function is useful for this. So let me comment out the previous read and print code and what I'm going to do is write some new code below this. I'm going to use the read lines function, so I'll write fl for file line is equal to my file dot read lines and then for X in fl, I'll print out each individual line, all right? So in this case, I'm reading the content as a list or an array of individual lines, and then printing them. Now, the net effect should be the same as you just saw in the previous example, I'm just doing it in a different way. So it's save and let's run, and you can see once again, that the text is being printed out to the terminal. In this case, cause I'm on windows and I had a manual new line character in each one of these text lines, you can see that there's an extra blank line being added. Don't worry about that for now. What's happening is the print function here is adding its own return to the end of the line. So just like almost everything else, you can see that Python makes working with file content really easy.

Contents