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