Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Identifying the Image Files

00:00 Okay, we’re getting to the meat of this. The next task is to move all the image files from the directory into the new images/ folder. What are all the image files? is a question that I would ask myself at this point. I’m like, okay, maybe there’s a .png file, maybe there’s a .jpg, but maybe there’s also other image files.

00:20 So this is a good time to head back over to the console and just take a look at all the file extensions, something we could do. By running the script, IDLE restarted the Python Shell, as you can see here, and that means that all of the variables that you defined before and all the imports you made inside of the REPL are not going to be accessible anymore.

00:41 Now what’s interesting here, though, is that if you type practice_dir,

00:47 you can see that you still get a response for that. And that is the variable that’s defined in the script. So what Python does, it basically runs the script with the -i option, which means that it executes the script but also puts it into the REPL that gives you access to whatever was defined inside of the script.

01:06 So that’s what happened here, and it might be a little confusing, so keep that in mind: that when you run a file with IDLE then the REPL session is going to end, and you’re going to be in an interactive session that is related to your script.

01:20 Now I want to look through this directory and maybe just take a look at all the file extensions that I have in there. So what I can do is I can say for path in practice_dir.rglob("*"): Let’s go recursively.

01:42 And I just want to print all the suffixes. So I’m going to say print(path.suffix).

01:51 Now there’s some folders in there, and the folders don’t have a suffix, so I’m going to get some empty strings as well. But it’s also going to give me all the file extensions.

02:00 So you see the empty results here are the nonexistent suffixes of a folder, and then I have a .csv file, .png, .txt, .csv, .png, .txt, .gif—or .gif, however you pronounce this—.txt, and a .jpg. Okay, so it looks like that’s an image type—.gif, then .jpg, and also .png.

02:24 So that’s the three extensions that I’m looking for. I’m already working towards my solution here. I’m already recursively going over the directory and accessing all of the items in there and looking at the suffixes.

02:38 So let’s build on that a little bit and just print out the relevant suffixes. I’m going to take a note of those here, with all image files to that folder, and that should be .png, .gif, and .jpg.

02:55 And I can continue working here in the console a bit just to try it out. for path in practice_dir.rglob("*"):

03:05 And I want to get, we can try out a couple different ways of doing this, but let’s start just, again, iterating over all of them and then using a conditional in here.

03:14 So I’m going to say if path.suffix

03:19 in and then I know there’s three strings that are possible that I want. These are going to have to be strings, so I put them in quotes, back,

03:33 going backwards here. Look at that.

03:39 Okay, I think I still managed to do it right. if path.suffix is one of these three, either ".png", ".gif", or ".jpg", then I want to print(path.suffix).

03:54 All right, and there you go. So we have access to those four image files with one of these three extensions. So that’s a little bit of testing in the console, and I’m just narrowing down the solution. I know I want to look for image files.

04:09 I looked a little bit what are all the extensions that exist, picked out the ones that are image files, and kind of like iterated on this for loop that I had before to insert a conditional statement that just filters out for those three file types.

04:26 I didn’t do anything with them yet. For now I just printed the suffix. But let’s change that in the next lesson.

Become a Member to join the conversation.