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