Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Concept: Reading JSON

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Lets quickly review loading data from a text file as JSON. So there's actually a couple of steps involved. We wanted to manage a global variable
0:09 so we had to say global rolls we're going to write to that ultimately. We need the JSON libraries so we imported that at the top.
0:16 We also saw that the working directory affects where Python looks for non-fully specified paths. Right, it's just rolls out JSON as well.
0:24 It's wherever you are and if that's not where yo expect things don't work. So we use the os.path module to do all sorts of cool stuff.
0:32 Get the directory from a file and then take a directory plus a file name and join it together in a operating system independent way.
0:40 That was cool, we came up with files. And then we wanted to use the open function pass it the file, r to read
0:45 set the encoding, store it in a variable fin. But we wanted to make sure we did that safely and closed it up no matter what
0:51 so we put it in this with block into a context manager. Whew! Now we're finally ready to load our JSON and that's just one simple line.
0:58 So we just say json.load, give it the file stream and boom, out comes the Python dictionary that was stored in that file.


Talk Python's Mastodon Michael Kennedy's Mastodon