Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Demo: Writing lines to a text file
0:00 Remember when we run our program our log messages, they come out here but they're not really going to a file that's why it says not really
0:08 and they shouldn't be going to the screen they should be going to a file. So let's work on this for a second here.
0:14 What we're going to do, is we want to write to a file we'll have a file name, remember this we tend to do that trick about os path
0:21 lets do it again, coz the same rules apply we want this file written where we would like it to be written.
0:27 So we're going to go over here and we'll just call this our rps.log. Let's call it like this, .log. Now in a real system you might cycle this by day
0:35 so you have a file with the date mixed in there but for now we're just going to do this. Now what we want to do is, we want to open a file
0:41 and write to it. So again, we'll do what we've been doing with open file name, now we're going to write to it we had W before but if you have a W here
0:49 and you open and close it and open again that replaces it. We don't want to replace the file we want to open it, stick a new line on the end
0:56 and then close it. So the thing we put here is a for append again the encoding is utf-8 as it's going to be an output stream so we say fout.
1:06 Now, we haven't really worked with this fout thing we've just been passing it off right hey JOSN go read or write from this
1:11 that's what we've been doing but this has some cool features. We can say read, which reads a character.
1:17 We can read line, which will read one line from the file. We can read all the lines, which'll load the whole file and memory.
1:23 None of these are going to work because we don't have an r we have an a. But we can write, we can write things or we can write
1:29 one piece of text or we can write a whole line. So what we want to write is, we want to write maybe the message.
1:35 Now if I run this something weird is going to happen. The message is going to pile up, one after another after another on the end.
1:42 The out on one gigantic ginormous line so we have to do another thing to say and add a new line, right.
1:49 Add a, a line break and in most programming languages it's kind of weird, you have this way of saying escape characters or special characters
1:57 you say backslash, it's not a real backslash that's the next thing that comes is a signal. You say \n, so \n means new line, like enter.
2:06 If you want a backslash you need two backslashes, there. So that was going to add a line we need to close and flush this file stream
2:13 but that automatically happens here. Okay this is a good start, let's just see what we got. Run it, notice the not really is gone.
2:21 Lets see Hannah's coming back in to try another round, she's going to throw a lot of sponge this time. Got beat with a sponge, just gettin' burnt
2:29 ah man crushed right away, as the computer is apt to do. So let's check in and see what's over here now. Look at this, rock, paper, scissors, log
2:36 and we open it up and check this out the app starts up, we've loaded this so that goes there, has us logged in. New game, look that's so cool.
2:45 So here if I just do it one more time. If I'm Michael, I'm just going to throw air at this computer, see how it likes it.
2:52 It's it's liking it, man that computer is rough! But notice down here now that we have the Michael round appended on, isn't that cool?
3:01 Right well, super, super cool I think. Let's do one more fun thing. Let's come over here and copy this path the entire path, go down here.
3:10 We can tail, -n, let's say N for history and -f this. Now you can see what's already there but I'll clear out the screen that's still running
3:19 I didn't stop it, it's just hanging out there. Let's put this over here this, and lets just run one more game.
3:26 I'm going to give Hannah a chance to pull- Look at that! The computer's ten to four against all of us. But look on the left, the app has started up
3:32 we've opened up those roles, cool. User two is logged in that's not what we want let's try that again. Hannah, Hannah's logged in
3:41 look at it just showing what's happening as its going. This is going to the files, super cool. We're going to just throw fire the whole time.
3:48 All right, Hannah takes the round, high. Computer takes the round, Hannah's doing again, high. Whoa look at that, Hannah won the game
3:55 and she's now ahead. We'd run it again, I'm going to say Michael you can see the log files just rolling along with it. One thing that would be nice is
4:02 if we're back here looking at this log file to know, hey this happened yesterday, or five minutes ago or these two things were separated by ten seconds
4:11 or something like that right? That needs some extra information that's not up to whoever's calling log, here, we add it on.
4:19 So what we can do is we can use a little bit of formatting here we can say, refix I guess we could call it. Going to be a little f-string
4:26 and lets go over here and just put a little bracket and we'll put the time in. What we want to use is datetime
4:33 remember that from one of your exercises? Normally it'll let us import it but maybe not inside of an f-string there we go.
4:39 So we want to import this one, the module not the class and use it over here now. Say date time, dot datetime.now.
4:45 When we run this it's going to have a whole bunch of stuff that we don't want, so we just want isoformat. Like this, this'll be a simple version.
4:53 And we're going to, let's just say instead of calling it a refix, we'll just write that bit. Here we go. Let's run it again, ah put it on the side
5:03 our hail is still running over here, that's cool. Our name is Michael, there we go for some reason that wasn't scrolling
5:11 I'm going to throw some more fire. Look at that how it has the time well maybe it's a little bit too precise you know maybe we should trim that back.
5:19 We could obviously come up with that. Maybe we just care about the day. Probably not actually what you want you probably do want the time.
5:25 But if you want to bring this back we could say today iso run it again and whoops not today sorry just date, just the date part.
5:34 There we go now you can see there's the date right there. So my name is, I'll give Hannah one more chance. She's pulling ahead, throwing a lot of water
5:41 look at that she's taken around, look at that. She crushed that computer, showed them. But here you can see, if you want to have
5:47 just the date you can do that, or you can do this. The point is not to re-create logging actually this is built into a lot of systems
5:53 and you wouldn't really do this. It's more to teach you about how to work with these text files. Use the file stream, write text to it
6:01 and then create new lines and add append to it. And that's what we're doing, it looks like it's working really well right?