Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Cleaning Up Your Script

00:00 With a working script, it’s time to do a bit of cleanup. And to stay in the theme of this lesson, I already cleaned up the terminal over here because it was getting a bit messy.

00:10 And now let’s tackle the script as well, because that’s what’s going to stay around. And the first thing I see in here that I want to clean are all of this code comments that I used basically just as crutches to help me get along and build this script. But now that it’s built and that it’s working, I don’t need them anymore.

00:29 They don’t add anything, and they just clutter up the code. So I’ll go ahead and remove those. I think that the code that we wrote here is straightforward and relatively descriptive, so we won’t need any sort of code comments to describe what’s going on in the code.

00:48 And really, these code comments were some sort of pseudo-code to help me develop the script. Okay, that looks better. Here the directory, "practice_dir", that’s still a bit different. Oh,

01:00 "practice_dir". I think this is called "practice_files". Yeah. Okay. So this is a little error here that I would’ve caught when running it, but let’s fix it right away. And I still have the task up top.

01:15 There’s not really any need to keep the task around, but it does contain a bit of information, so my idea would be to rewrite this as a script docstring at the very top.

01:28 That gives other people—and yourself in the future also—the opportunity to quickly read and figure out what this script is about and what it’s supposed to do.

01:37 So let me rephrase this in a sort of a docstring manner. I’m going to call this script Image Mover.

01:48 This script finds image files recursively in a folder

01:59 structure and moves them to an `images/` folder.

02:09 What else? Okay. We have this exist_ok=True, so might be a good idea to note that. It creates the `images/` folder if it

02:24 doesn’t already exist. Okay, so I think that nicely describes what this script does at the moment, so I’m happy to get rid of this task description too.

02:36 And that already looks much cleaner to me. So the last comment that’s still in here, this is a bit different because I had this in here so I could quickly swap between the copy_dir/ and then the practice_files/ directory, which is the actual one that you want to run this code on.

02:54 We didn’t make a lot of use of this functionality, but now at the point when we are are stepping out of the testing phase and into actually running the code, this might come in handy.

03:04 So I’ll leave this in for now and maybe do a little bit of refactoring before we are ready to completely finish off the script.

Become a Member to join the conversation.