Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Demo: Moving the rules to a file

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's jump over to our rock, paper, scissors game. Now I have a new project here. I've made a copy. You can see over into chapter nine.
0:07 Working with files in the GitHub repository. This is just a copy from CH08 or CH07, whenever we worked on it last. Now here we are.
0:14 It opens up actually right to the subject of this whole demo here. We want to take this code which right now is an RPS game.
0:23 And it works fine, this is a Python dictionary right here. And what we're going to see is that it's fine here but if you want to extend it
0:32 you know you got to change the source code and that's not really what we want. We want a separate file that anybody
0:37 who's even not a programmer can mess with and then we can try to load that up. So what we're going to do is we're going to go over here
0:44 and we're going to create another file. We're just going to write this by hand and then we're going to load it.
0:49 Later we're going to create a file with Python but right now we're just going to read a file. So I'm going to create a file called rolls.json.
0:56 We get to pick the name. Call it whatever you want. I'm going to call this one rolls. Now it's unhappy 'cause it's not structured JSON.
1:04 JSON basically follows the same rules as Python dictionaries for the most part so what it wants is something like this and that'll make it happy.
1:11 So let's just go over here and copy this bit. We don't give it the name we just take the value or whatever and put it over here.
1:19 Now all of a sudden it's freaked out. It looks really broken. Like everything is broken. Well JSON is more picky than Python.
1:26 Remember in Python, we can have single quotes or double quotes. JSON, it only likes the single quotes. So let's just replace all those.
1:32 And ta-da, it's magic. The other thing is that Python allows you to put the comma on the last item. This is nice so that as you add new items
1:41 it doesn't, you don't have to worry about this but JSON also doesn't dig that. Other than that, here we are. We have this working.
1:49 So let's go and delete that. We can maybe set it as empty just for starters. But let's go over here and we can do something like
1:57 write a function or express there's going to be a function called load rolls. And we're going to go put that down on the bottom.
2:06 Now one of the things we got to deal with that's new for us is you see this rolls is outside of function? In order to work with it
2:14 specifically in order to write to it 'cause we did work with it in other places but to write to it we have to say first global rolls.
2:22 And now once we've done that we can say rolls equals whatever. If we didn't have that then you get a warning that this one here
2:29 is shadowing, hiding the outside one. You're not actually working with that. Okay so what's going to be the file name? It's going to be what we typed.
2:37 Check that out, how sweet is that? PyCharm finds it for us. Thank you PyCharm. The file is this. Now we're going to start with just assuming
2:44 that everything is kind of lined up next to each other and then we're going to improve upon this. So what we need to do is we need
2:50 to go and use a library called json. So import json. And JSON knows how to take this file format and turn it into a Python dictionary
3:00 exactly like what we had before. But what we have to do to make that happen is either give it text which we no longer have or give it a file stream.
3:09 So first thing we got to get is a file steam. We'll say file input. Going to be like that, so you just have this built-in function called open.
3:17 See it takes a file name a mode, this says is it read, write is it text, is it binary are you appending to it and so on. And then encoding.
3:25 We want to go over here and we're going to say filename. The mode is r for read. And defaults to text. If we wanted binary it would say rb.
3:34 We're going to set the encoding to utf-8. That's a pretty safe bet. And then we're going to say rolls = json.load. Now these names are not super great
3:47 but load means load from a file. See that fp, the file pointer. Why couldn't they say file pointer?
3:54 I don't know, they don't need to save that much space, so. That's what we're going to do. If you had a string you would pass the string to loads.
4:01 Now that wasn't too hard was it? We're going to open this file. And we're going to work with it. Now the next thing we need to do is
4:06 make sure we close it here. Otherwise that file might get locked something else tries to open it
4:12 it can't because this file pointer has it open and so on. There's going to be some improvements around that but let's see if it's working.
4:17 Remember up here we have nothing here right now. Right we're going to wipe this away. So what we're going to do let me just make a super minor change.
4:27 So over here. Nah, nah I'm not going to change it. But notice we're just going to load this up and it should work the same.
4:33 If everything works let's just say do a quick printout here. Let's do print. load_rolls. We want to put basically the rolls. Not keys.
4:46 It'll show us that but I don't know that it's going to come up right so we can say let's try this. Let's try it and see what we get. Dict keys.
4:54 I'd rather have it just be a let's just do a list. That'll make it look nicer. Check that out. We loaded those rolls from the file.
5:01 Remember that's not what's up at the top. There's nothing here. Super cool. So now let's just make sure that we can still play it.
5:08 I'm going to play a rock. Okay I played rock. Computer played scissors. I took the round, awesome. Going to play rock again.
5:15 Going to go straight rock on this one. They played rock. Ooh they beat me with some paper. They beat me with paper. They beat me with paper, wow.
5:22 My decision to go straight rock was bad. Nonetheless though the game it's unchanged right? It doesn't seem like anything has changed
5:30 because we were able to load the same information up here. Yeah, very, very cool. But now the difference is whoever wants can just go edit this file
5:39 and that's going to define how the game works. Specifically they can add things they can take away things change what beats what. Mix it up, get crazy.
5:47 So it's a really cool way to do that. And look how simple it is.


Talk Python's Mastodon Michael Kennedy's Mastodon