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

Moving the Files

00:00 So are you ready to run the script? Go ahead and press F5, save the file, and the script ran. I don’t know whether anything changed, but let’s head over to the file system and take a look. Inside of copy_dir/, now I have the images/ folder, and all the images are in there.

00:19 Well, that’s great. Look at that. The script worked as expected and moved the images from the original locations. They shouldn’t be in here anymore. No image files anywhere, so that looks good.

00:37 Let’s double-check in some code on the REPL as well. Let’s do this similar to how we did before. I’m going to search through the directory that is not images, so inside of the documents directory, and let’s figure out whether there’s any image files left in there.

00:55 documents_dir was inside of practic_dir and then "documents". This is a sibling folder to the images/ folder where I moved things.

01:09 And then I can loop over this. for path in documents_dir.rglob("*"):

01:20 if path.suffix in one of those three,

01:29 then I want to print the Is there anything in there? Nothing. And if I run the same thing for files, files_dir = practice_dir and—oh no, not files, sorry. It’s called "images". "images". There you go.

01:52 for path in—well, okay, okay. I know I’m just riffing here on the REPL, so it doesn’t matter that much. But I kept talking about a files_dir. It’s actually an images_dir, so let me rename it.

02:08 That’s what we actually want for image in images_dir.rglob("*"):

02:21 and then copy this. If path is in one of those, then print it out. Oh, I don’t have anything in here, so I did something wrong here. Aha, that’s a classic copy/paste error here.

02:39 I renamed this to images to be more descriptive, but then I copied the old one, which iterates over path, and so that variable doesn’t exist.

02:46 So let’s do this again, but do the necessary changes.

03:00 And here we are. All four image files now exist inside of the images/ folder, and they’re not present anymore in the documents/ folder where they used to hang out in before.

03:11 This might be a good point to move over from copy_dir to practice_dir. But before you do this, in the next lesson, let’s do one last test round and also clean up the script file a bit to remove all the code comments that you used for development, and then finally also run it on the actual directory structure.

Become a Member to join the conversation.