Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 6: LOLCat Factory
Lecture: Creating and detecting directories

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right, back in PyCharm, it's time to build that Cat Factory. So, like always, we will add a new Python file
0:09 to get started and in our convention, we'll call that program.py. Let's go ahead and define our main method as you guys should be familiar with by now
0:19 so we'll create our main(), and down here let's do a few things I'll just sketch this out in comments
0:24 so let's start by saying print the header, as usual, next we are going to get or create output folder and then we'll download cats
0:35 and then finally, let's go and actually display cats. Now just so that we can sort of see things running
0:42 I'll just say hello from main, just for a moment, so we can get everything executing just fine and remember, we have the live template in PyCharm
0:50 that gives us the correct pattern for launching our main method only if it's being called as the program itself
0:56 so here we'll just say main(), nothing too fancy there, and let's go and run it once to setup the run configuration boom,
1:04 all right, so everything is working, the first thing we've got to do is print the header,
1:09 that is pretty straightforward, so we'll just say def print_header(), and you guys are really familiar with this by now
1:15 so let's say print() and it's this sort of thing, do a little duplication, and we'll call it Cat Factory.
1:22 Something like that, how about with capital O, Ha?. So we can go over here, let's get rid of this useless print,
1:28 and let's go up here, we'll call print_header(). So the next thing we are going to do is we are going to work with our output folder,
1:34 so let's try the method down here, called get_or_create_output_folder(). All right, so now we are going to use our OS module and the path class
1:49 so we are going to come up here and we can have PyCharm do this or we can say import OS and down here we have to pick what our folder is called.
1:56 So we'll say folder equals, and let's just call it cat pictures, something like that to make it really obvious,
2:01 and then we'd like to create this folder first we'd like to figure out what the full path is
2:06 and then we are going to either create it or just return it if it already exists. So let's say full_path = os.path.join()
2:15 and we could start by joining just this in the folder, and let's just for a moment print that out,
2:22 you guys have already seen let's make sure we call it so that something actually happens and if we run this you can see we have ./cat_factories
2:31 and before we were actually able to come up with the full path and that was actually working out pretty well, we were saying os.path.absolute path,
2:44 and we got our full path to where this folder that doesn't yet exists was. However, the fact that we say . really is a comment on the working folder,
2:55 not relative to this particular Python script, so I would like to have this folder called cat pictures, right next to my program.py file here,
3:04 we could do something different, let me just do a print statement here, you saw that there is __name__ that is a special string that is defined right,
3:13 we are using that right here, and it's __main__, it's literally what it is the way we are running it now or it's going to programmed.
3:21 But there is another one that we could work with called file and you can see that here is the file that we are currently in,
3:29 the module that we are currently running in regardless of what the working folder is, so we can actually get the base folder, we can get it like this,
3:37 we can say os.path and just call dirname() and give it this file here like so and instead of using this absolute path
3:48 and all this stuff we can just go back and say we are going to combine base folder with our little local folder,
3:56 and you should see we have the same output but this time it's sort of independent of the working folder. So we now have our folder,
4:04 the next thing to do is create this folder but only if it doesn't exist yet, so we'll say if not os.path.exists give it the full path
4:14 this is the folder that we are going to work with but maybe this is a file, maybe somebody has created a file called cat pictures here,
4:20 we'd like a folder as well so we could say or and come down here and say isdir() and let's put a not here, Ok so now we want to create this directory,
4:32 add full path and if this had many levels in it that didn't already exist you would have to kind of do this recursively, but we'll just say os.mkdir(),
4:40 remember it's not on the path it's actually on the OS model directly and we'll give it the full path, and we would kind of like to know
4:46 did the thing find the path before or did it actually create it, so let's do something like this, we'll say print creating new directory at
4:55 and we'll put the little location here, so only when we are creating the directory shall we see this print statement,
5:02 but either way we should return the full path, now let me leave this out just for a second to show you some PyCharm goodness
5:09 so if I come over here and I create this variable I want to say this could be the folder, like this,
5:15 right away PyCharm says no no no this is a bad idea, it looks like you are trying to store the return value a function into this variable folder,
5:23 yes we are, but if you look carefully it will say this get or create output folder method does not return anything,
5:29 in some languages this would crash or not compile or something, but in Python there is an implicit return value for all functions,
5:36 every function returns a thing, but if you just omit their return statement, it's only going to return none, so it's not super useful.
5:42 That's kind of a warning that oh, woops we forgot to return full path, regardless of whether we created it. Now let's just do a print really quick,
5:55 we'll print so found or created folder and we'll just put a little folder, ok,
6:00 now let's run it, if you look over here there is no cat pictures directory, but if I run it it says creating new directory here and then it says
6:09 found or create a folder there yes, and now you can see PyCharm's automatically refreshed and found hey, there is cat pictures here, if I run it again,
6:16 we should only see the second found or created but not the actually creating line. Perfect, so now we don't recreate it and actually
6:24 that would have crushed had we tried. That's the first half of our os part we've created this folder, we are ready to put our binary data into it,
6:31 the next thing we have to do is go download some cat data. Before we do that, let's just hit a little command alt+L
6:37 to do some cleanup and have our code look nice and fresh.


Talk Python's Mastodon Michael Kennedy's Mastodon