Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  • Overview
  • Transcript

3.1 File Basics

Files are some of the most fundamental aspects of any computer system. Unfortunately, in this day and age, they are often overlooked as second rate citizens to fancier storage mechanisms like databases and the like. I'm not saying that files are better; I'm just saying that it is important to understand them and how to use Python to get access to them. In this lesson, I'll show you how to do just that.

3.1 File Basics

Now I feel like it's time to take some of the training wheels off just a little bit. To this point in the course, when we've been working with data or collections or lists or things like that, we've been working with data that we've kinda been just kinda putting together within our code. That was a little contrived, and we just kind of threw some data together without really having it mean much of anything, just as an example. Now out in the real world, you're not gonna run into a lot of scenarios like that. When you're dealing with data out in the real world, you're going to be having to retrieve that data from locations, process it, maybe save it to other locations, read it here, write it there. And those are important things to learn as you progress in your Python journey. So what I'd like to do is spend a little bit of time in the next couple of lessons walking you through the basic process of dealing with persisted data, both reading it and writing it, when it comes to files. Now files are a very common way to use Python. When I use Python, it typically has to do with reading data from, say, log files or crash dumps, and things like that, and picking apart and grabbing certain pieces of information that I need so that I can do my job better. So what I would like to do is take you through the process of going through files, reading data, writing data, seeking around, and getting into certain areas of those text files or of those binary files, possibly, and getting data in and out. So what I'd like to do is start with this lorem.txt file. And this is simply some lorem ipsum text that you would typically see on a website or a web page as you're designing or building it. That's going to contain some just placeholder text to see how the text is going to flow. But it's a good example for us to start with when we come to reading and writing data to text files or to files with Python. So the first thing we need to talk about is getting at that data that's already existed there. So the first thing that I wanna do is, I wanna be able to read that file and present it to the end user. Now, there's a number of different ways to do that. But let's start off very simply by simply opening the file and reading the data a line at a time. So how do we do that? Well, as I said, there's several different ways to do that. But typically, what you're gonna find is using the open function. Now, the open function is gonna take in a couple of parameters, one of which is required. And that required one is going to be the name of the file that you want to open. Now, if you're in the same directory as that file, like I am, you can simply say the name of the file, so in this case, lorem.txt. If it's in a different directory, then you're gonna have to specify the full path to that particular file. Now, doing just this is going to open this file and return an object, so open returns an object. And you might hear this referred to as a file object, a file stream maybe, a file handle, but at the end of the day, it's basically a reference to that file so you can do things and manipulate that file. So what I like to do is, I like to specify the file name or the reference to that file as some sort of descriptive text_file. So not only do I know it's referring to a file, but I know almost which file it's referring to, or what data is in that file, in case I have several files open and I'm comparing or I'm grabbing data from one and writing it to another, things like that. So by doing just this, I'm going to open up the lorem.txt, store a reference to that file in lorem_file, and this file will by default be opened for read access. Now, why is it open for read access? Well, because that's the default of the open function. Now, the second parameter here that we can specify is the access mode to which we have to that file, and by default it's read. You can explicitly state it as being read by specifying an r, and there's other access modes like write, write binary, read binary, and things like that. But we'll get to some of those a little bit later on. We just wanna start with the basics. So if you wanna open it to read the file, you can either specify explicitly with an r as the second parameter. Or you could just leave it off, that's pretty common as well. So once we have this file opened and have a reference to it, we can start to do things with it. So like I said, the first thing that I wanna do is, I wanna print out each line in that file and show it to the end user. So the simplest way to do that is to iterate over this lorem file variable, just like we've been doing in other lessons. So I can say things like for line in lorem_file, and I can print out each line. So that's gonna give me a string representation of each line within lorem_file, it's gonna print that string. And once I'm done with this, one thing that you need to remember when you're dealing with files, especially when you use the open function, is that you always need to close it. Now, the reason you need to close it is because you have an open reference to an object here, so to our file, we have a reference to that file. If we were to not close that file and leave it open, we can cause problems for other people trying to access that file, as well as introduce the concept of a memory leak. Because we have an open reference to that file, we're not cleaning up that reference, we're not getting rid of it, it's gonna stick around forever. And if we continue to open up files like this and not close, then we could run out of resources, run out of memory, and have a nasty crash. So please always remember, if you use the open function you need to close it as well, simply by specifying the name of your variable that contains that reference and close, just like that. So let's go ahead and save that. Let's hop over to our shell here, and I want to run our script. So I want to say python3, and I wanna run file_read.py. So let's go ahead and do that. And as you can see here, it printed out all the lines here, one at a time. And it really took up a lot of extra space here with all these extra lines in between, because it printed out the new lines that were associated with each of those lines. So let's go ahead and strip those off. So we can do that by using a very simple function on the string object here on this line called rstrip, which is gonna strip off those new lines here. So let's quickly run this guy again. Actually, let's clear this, and then we'll run that again. And as you can see, now it prints it out all nice and condensed for us, one line at a time, as you can see here. So that's the basics of getting in and opening up a file, getting the contents out, and then being able to present them to the end user, and then making sure we close the file.

Back to the top