Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: 01-file-intro

Login or purchase this course to watch this video and the rest of the course contents.
0:00 It's time to add some permanence
0:02 to our program, so far what we've done is we've created
0:05 a couple cool games and some challenges
0:07 like that guess the M&M count
0:09 but there was no memory, there was no history
0:12 there was nothing permanent about what happened in the game.
0:15 You played it and then it vanished, it was over.
0:17 So what we're going to do in this chapter is we're going to work
0:19 with File IO and different file formats so that we can save
0:24 a history of what's happened in our game.
0:26 We're going to create a leaderboard
0:27 for our Rock, Paper, Scissors game
0:29 but that's just interesting, that's kind of fun
0:30 it'd be cool if you're in the lead and whatnot
0:33 but what's really going to be powerful is we're going to allow
0:35 people to extend and modify the overall gameplay
0:39 of Rock, Paper, Scissors entirely just by creating a file
0:43 and using it instead of typing stuff into our source file.
0:46 It's going to be both easy and amazing.
0:48 So, I think you're going to have a lot of fun
0:50 in this chapter and it's going to really be
0:52 a big bang for the buck for what you get.
0:55 Now let's just talk about File IO in general, real quick.
0:58 There's all different kinds of files that
1:00 we might want to work with, a very common one is some form
1:03 of text file, this might have some kind of entries in it
1:07 you can pull it apart and work with it that way
1:10 so you're reading basically lines of strings
1:13 in these text files or you're writing them for other people.
1:16 One special version of this might be a log file
1:19 this is like a history of what's happened in your program.
1:22 Today the game was started and then player so and so
1:26 decided to play a game and then they played rock
1:28 and they lost and they played scissors
1:29 and they won and now such and such, right?
1:30 It's just a kind of a history of what's happened.
1:33 This is really important in websites and database systems
1:36 and API and things like that because you want to know
1:39 what's happening in your program, you're not the only one
1:41 using it, all the people on the internet are doing things
1:44 usually good things, sometimes they're trying to abuse it
1:47 and sometimes they're just problems and things don't work
1:49 right and you want to go back to these log files.
1:51 So, we're going to see about creating those.
1:54 Now, those are just sort of free form things you put in
1:57 as text and you can edit them with some kind of text editor.
2:01 We're also going to work with dedicated
2:03 structured file formats, something called JSON
2:06 these are very, very similar to Python Dictionaries
2:08 but they get saved and loaded into files.
2:12 This is traditionally used for exchanging data
2:14 with some form of web service, they expect this structure
2:18 data and they can pull it apart, but you'll see it's also
2:20 going to be really useful just for working with files for us.
2:23 Another very common format is, I've got some kind of table
2:26 of data, so you can create a comma separated value file
2:29 this is a special version of a text file as well
2:32 but it lets things like Excel and Google Sheets
2:35 and other spreadsheet like software work with this.
2:38 And these are extremely easy to create in Python as well.
2:41 And finally, we might come across some form of binary data
2:44 which you have to create slightly differently
2:45 like maybe we want to create an image
2:47 or a media file or something like that.
2:49 So there's a wide range of formats in Python.
2:53 What we do to work with them, there's some commonality
2:56 across all of these, but there's also some uniqueness.
2:59 The way that we work with text files, for example
3:01 is different than with binary files like a JPEG.
3:04 Also, the way we work with JSON data might be different
3:07 than the way that we work with a log file
3:10 but you're going to learn about the general principle
3:12 behind all of these, and we're definitely going to work
3:14 at least with JSON and maybe TXT as well.