Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 6: LOLCat Factory
Lecture: Downloading and writing binary data

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now that we have this data downloaded form the internet, let's go and write it to our file. We want to save this data to a particular name
0:09 related to the cat name in the folder that we pass so this should be pretty easy to write, we'll say save image and we'll give it the folder
0:17 we'll give it the name and the data. So down here we are going to have a method like so now you worked with files before and you know
0:27 when you are just going to use the file stream just for a moment and then you are going to close it right away
0:33 you want to use what is called a context manager so we'll say with and open() is how we get all the file stream,
0:38 and we want to give it a name so let's come up here and say file name = and again, we are going to use the os model
0:43 so we'll say os and import that at the top, we'll say path.join() and our folder is in absolute path so that should work
0:52 and then we have our name and then this is like the name of the cat like lol cat 1 lol cat 7, these are basically jpg's
0:59 so let's just add on the file extension.jpg. So now we can say open file name and say as fout for output file stream,
1:08 and then we are going to somehow work with this, if I were to just write this code and try to write this data to it,
1:15 this data is binary, this by default is a text file it's not going to turn out so well for us
1:22 so what we need to do is go over here and change the mode, and we can say I'd like to write, by default it's read only
1:28 and I'd like to write not just text but binary so we'll set the mode to wb for write binary and then I could work with the output stream
1:36 and the data stream and sort of copy those around and juggle the bytes and that would be fine
1:41 but it turn out there is a module called shell util that will help us out here.
1:46 So we'll say shutil, like so and we want to import that at the top of course, and it has a method called copy file object.
1:55 So we can say I would like to copy this data stream to that data stream, done, one line, beautiful.
2:02 Now, PyCharm says you should really move your method down one line, according to pep 8 so there we go.
2:08 All right, we are ready to run this app and get some cats but remember I told you on line 14, 15 here we are doing it wrong
2:15 so let me run it, get some sort of not working images, and then I'll show you how to fix it, it's really easy but it's also easy to overlook.
2:23 Let's make sure we get some output while we are running our download so we'll do a little print statement here,
2:29 we'll say contacting server to download cats, and we'll say something like print downloading cat and then let's actually involve the name here,
2:45 I'm going to say plus name, and then at the very end I guess we could print done, something like so so let's run it.
2:57 Ok, how exciting, let's go look at our cats, so now you can see we have all of our cats and if I open them up, that's not a super cool cat,
3:05 I love all cats and I was hoping for a good little laugh when I saw this picture, and this made me sad,
3:11 well you might think there is a problem with PyCharm but in fact, if we actually open this up and find her you would see
3:16 there is not really a proper picture there, so let's go back here and talk about the mistake we made when downloading binary data using requests
3:23 so in order to work with this raw stream here in the manner that we were doing it,
3:28 we need to go tell request to operate in what is called streaming mode, so we can say stream = true, and now if we just rerun it
3:34 you should see some nice cats. Ok, it's all done, let's go look at the cats. Nice, trash cat is not amused, but I am, because this is awesome,
3:46 let's look at one more, uuu I'm in your quantum box, maybe. Ok, so perfect, we've downloaded our data using requests
3:53 and we are working with the raw stream and we set stream mode to be true we then pass that stream over to our saved image
4:01 which we create a binary file we can write to and we use the shell utility copy file object to copy from this stream to that stream.
4:11 But as a user, how would I know my cats have been downloaded other than seeing done, ok, great, done, but I actually want to see the cats
4:18 so the next thing we are going to do is show the cats to the user.


Talk Python's Mastodon Michael Kennedy's Mastodon