Identifying the Image Files
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.
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.
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.
So you see the empty results here are the nonexistent suffixes of a folder, and then I have a
.gif, however you pronounce this—
.txt, and a
.jpg. Okay, so it looks like that’s an image type—
.jpg, and also
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.
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
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.
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.
Become a Member to join the conversation.