Python for Absolute Beginners Transcripts
Chapter: Course conclusion
Lecture: Review: File I/O
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Once we rewrote our Rock Paper Scissor game with data structures it turns out it's pretty easy to save and load those data structures from files.
0:10
The next topic we covered was File IO and File IO usually starts around the open function. The open function creates this file string
0:19
either an input or output string and you need to make sure it gets flushed and closed properly so you want to put it into a width block.
0:26
That's what we're doing at the bottom of this screen here. With open, give it a file, this one's for reading
0:31
so we say R, specify the encoding just to be explicit there and we give it a variable name, Fin. I like fin for file input string
0:39
so the name tells you, you can't write to it and here we're actually going to load up a JSON file so we don't need to understand how JSON works.
0:46
All we need to do is input the JSON library and say, hey, here's a file string to something you understand. Go get us the data, put it into rows.
0:53
That will be a dictionary on the Python side of things. We also saw that working with paths is tricky and knowing where you're actually located
1:01
can be a challenge. Remember, this is based on the working directory. And trying to put these together and work with them
1:07
is also tricky in a cross-platform way. Windows and POSIX macOS, Linux systems have different separators and definitions of how you put files together.
1:17
That's why we use the OS.path module because it knows how to do this for us regardless of the operating system we're on. This is just one of the things
1:25
we did with files throughout the course and I think you'll agree they added a lot to our little program.