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