5.4 Read files - Video Tutorials & Practice Problems
Video duration:
6m
Play a video:
Video transcript
<v ->So now let's quickly look at how to read and write</v> to and from files. So we've got, this challenge_3c_word_guess_from_file, that is open, and then there's also this words.txt file here, and if I open that up, I have a txt file with like a whole bunch of words in it. 213. So, if instead of having a list of words at the top of the file, and I wanted to instead read the words from words.txt and pick a random one, this is how you would do it, or this is how I did it. I created a new function, called pick_random_word, and to get the answer I now call that function. That function is going to return a word. But here's where we do the magic. We have this new keyword called with, and it's hiding a little bit of interesting information. So when you're looking at files, it's pretty much always gonna look like this, and, it'll say with, and then open, you're opening a file name, you're opening a file, the file has this name, and you're in this mode. So for 'r' that's just reading mode, and it means that we're not gonna write to the file. And then this keyword as, and file. And then we're going to indent after the colon. You don't have to fully understand what's happening here. The main things are that you have to put the right path to the file that you wanna open. So right now, words.txt is in the same folder as the file I'm calling it from. If I was trying to get it from within a folder, within another folder, like... I could put the folder name, and then slash, and so it would look inside of the folder with that name, and then look for this file. And then if I wanted to look in the parent folder, and go out of the current folder that I'm in, then I could use the two dots, and then I could, you know, look for a sibling folder, and then find the file. So this line will fail if it can't find the file that you specify. And then you have to have the right mode. So for reading it's 'r'. And you're creating a new variable, and so the file that you open is just gonna be set to this variable. When you deal with the file that's all inside of the indentation block, and then once you go out of the indentation block, it actually closes the file so that you don't have any errors from having that file still open and being read. So that's why there's a little bit of magic happening here with this with keyword. It's actually closing the file, so you don't have to remember to close the file. So now, once we have that file, we can read the lines from it, or we can loop over and read line by line. Let's see, there are a few things... So we could close it ourselves. We can read, we can read a single line, or we can read many lines. So read lines is not ideal if you have a really large file. For that you would probably want to read line by line. But I think this is a small enough file that we can just get all of the lines at once, and that will be creating a list of lines, and each line has a word in it. So then we can use random again, and choice, to pass in a list, and that will return a single line, and then we'll use strip because each of those lines also has a new line attached to it. That will get rid of the new-line character at the end. So if I run this... Or actually let's change this to actually adjust... (clicks) pick_random_word, (clicks) and see what happens. nymph, wristwatch, grogginess, and then we can print some stuff here. So, like let's print what the value of words is. And you can see that new line character, in each one. And if we didn't do strip at the end, then we would actually get espionage, and a new line. Yeah, so that's basically how you open and read a file. There's different ways of reading that file. We're just doing a very simple way here. And depending on what's in that file, you will wanna do different things. So if it's a comma separated value file, you'll want to break it down into a list of values, and split wherever there are commas. So there's different functions for doing that as well.