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
0:03
with data structures
0:04
it turns out it's pretty easy
0:06
to save and load those data structures from files.
0:09
The next topic we covered was File IO
0:12
and File IO usually starts around the open function.
0:15
The open function creates this file string
0:18
either an input or output string
0:20
and you need to make sure
0:21
it gets flushed and closed properly
0:23
so you want to put it into a width block.
0:25
That's what we're doing at the bottom
0:26
of this screen here.
0:27
With open, give it a file, this one's for reading
0:30
so we say R, specify the encoding just to be explicit there
0:33
and we give it a variable name, Fin.
0:36
I like fin for file input string
0:38
so the name tells you, you can't write to it
0:40
and here we're actually going to load up a JSON file
0:43
so we don't need to understand how JSON works.
0:45
All we need to do is input the JSON library
0:47
and say, hey, here's a file string
0:48
to something you understand.
0:50
Go get us the data, put it into rows.
0:52
That will be a dictionary on the Python side of things.
0:55
We also saw that working with paths is tricky
0:58
and knowing where you're actually located
1:00
can be a challenge.
1:01
Remember, this is based on the working directory.
1:04
And trying to put these together and work with them
1:06
is also tricky in a cross-platform way.
1:08
Windows and POSIX macOS, Linux systems
1:12
have different separators and definitions
1:14
of how you put files together.
1:16
That's why we use the OS.path module
1:19
because it knows how to do this for us
1:21
regardless of the operating system we're on.
1:23
This is just one of the things
1:24
we did with files throughout the course
1:26
and I think you'll agree
1:27
they added a lot to our little program.