Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Concept: Appending a line to a text file
0:00 Let's just quickly review our simple little log thing. Where we were able to write out some kind of message and append it to a text file
0:10 that is just line by line interesting. So we wrote this log function it takes some kind of text
0:15 and we use our os path to come up with the absolute path. And then we come up with some form of time text. Here we're using this string format time.
0:23 Which gives us actually, a better output than what I was doing. Then, we're just going to use our open method inside of a width block
0:31 get a output file stream out. I name it so it reminds me yes you can write this but not read from it or read from it, but not write to it
0:39 based on how you opened it. Cause the functions are there for both. But depending how you open it one of the other is available.
0:45 But the most important thing here is that we used a to append rather than w to overwrite. And then we just write out parts of text
0:53 and it piles up all in one line so we'd write the time text then we'd write out the message that they're actually trying to log.
1:00 So they don't have to worry about the timing. And then we say that's the end of this line wrap it around, so that the next time
1:06 we try to log to it it starts on a line below. And that's it. And here we're also printing the message. So you can both log to it
1:13 everything that goes to the screen, basically will also go to the file. If you wanted. We did ours a little bit different in the demo.
1:20 Not too hard, it reuses a lot of the same ideas. But very very cool, and quite easy to do.