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

Considering Next Steps

00:00 So the previous lesson, I moved the name of the folder practice_files/ back into the FULL_PATH constant towards the top of the script. And I mentioned there is a reason for that.

00:11 And my reason would be to go towards generalizing the script somewhat more and also thinking of what is an outlook of what I might want to do with this script moving forward.

00:25 I quite like the functionality of this image mover, and I want to be able to use it not just in the python-basics-exercises/ directory with the practice_files subdirectory, but instead I want to be able to use it on any folder on my system, let’s say. The functionality is nice.

00:43 It creates a new images/ folder and just moves recursively all the images in there. So I might want to use this somewhere else. And by putting the whole path into this one variable up here, I’m kind of keeping it contained. And for me at least, it makes it easier to think how I would touch the script again to change it.

01:03 And I might want to use the argparse library to collect user input using the command line, and instead of defining the path right here inside of the script, I would allow the user to give the input from the command line. So I would then just need to replace this FULL_PATH variable here with some code to collect it from the command line. And similarly, I could do the same with the image extensions.

01:30 So maybe the user can define also from the command line which image files they want to move. So having these two pieces of information, the path and the types of extensions that I want to move towards the top of script makes it more accessible and maybe easier to build forward, to build on top of, and keep it general. That’s my reasoning for doing this.

01:53 And I think the script looks great. The code is relatively short. It is quite descriptive. Yeah, it moves towards generalization as well. And you also added a script docstring at the top of the script that describes what this script does.

02:11 So everything’s looking good and I think it’s time for the final run to actually see whether it’s now going to also move the files inside of the practice_files/ directory. So let’s do it.

02:23 The honors of pressing F5 for the last time. You see that we get a restart over here in my cleaned-up Python Shell, and I can move over to my folder structure, take a look, and here’s the images/ folder, and it’s filled up with all the images.

02:39 Perfect. The script works. So this is what I came up with. I’m curious to see what is the code that you wrote. If it’s different, remember that doesn’t have to be wrong. Post it in the comments below, and I’m looking forward to see what you came up with.

Avatar image for alnah

alnah on Sept. 18, 2023

from pathlib import Path

PRACTICE_FILL_DIR = Path(__file__).parent / "practice_files"

images_dir = PRACTICE_FILL_DIR / "images"

for image in list(map(str, PRACTICE_FILL_DIR.rglob("image*.*"))):
    destination = images_dir / image.split("/")[-1]

Too much complicated I would say. I did not know the suffix attribute from Path objects.

Become a Member to join the conversation.