Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 4: Journal app and file I/O
Lecture: Core concept: File I/O

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So that brings us to our next core concept, file I/O. And here you can see we have a list of three text items
0:07 and imagine we want to just write these to a file. So, if we’re going to use that file stream, briefly
0:13 and then when we exit this little segment we are going to close it, we'll use this context manager so we'll start out with,
0:19 create the item as variable name. So, in this case with open and we pass file name and w for write text format, and we want to say as fout,
0:31 for that's the variable I am calling it output file stream. And then we just do whatever we want to do to write to the file,
0:36 so in this case we want to write these three items on three lines and this text file so we just for-in loop over them
0:42 and we say fout.write() the text and as you saw, that does not put a new line on there as it should not, so we have to add it with this \n.


Talk Python's Mastodon Michael Kennedy's Mastodon