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

Creating a Subdirectory

00:00 Time for a test run. Now what I said before is that I don’t necessarily want to test directly on the practice_files/ directory, but instead I’m going to use copy_dir for that.

00:10 So since I might change these out a couple of times while I’m testing, I’m going to instead of defining the path to practice_files/ here, I’m going to put a constant here. I’m just going to say, I’m going to call this DIRECTORY.

00:28 And that’s a variable that I’m going to define up here.

00:33 I’m going to say DIRECTORY is later it’s probably going to be

00:40 "practice_dir". That’s what I eventually want to use. But now I’m going to comment that out because for now I’m going to work with "copy_dir". So this is the directory that I’m going to use for now, and it’s going to be easy for me to change between these two by just commenting, uncommenting, or I could replace the string in here that points to the actual directory that I want to use. For now, it’s going to be copy_dir/ here at the end, which means that whenever I’m going to run this script, it’s going to, yeah, use this copy_dir/, which is my testing directory, so to say. Cool.

01:16 And now I want to test what I wrote so far and figure out whether running this code, the script as I have it so far, whether this is going to create a subdirectory called images/ inside of copy_dir/. Now, in order to be able to run this, I need to first save the file.

01:35 So I’m going to press Command + S to save it,

01:40 and I’ll put it into this python-basics-exercises/ directory and call it move.py.

01:50 Okay, so I got it saved, and now I should be able to run this code and see the images/ directory pop up. To run it, you can go to the menu, click on Run, and then Run Module, or you can press the shortcut F5.

02:07 I’ll be using F5 to run the code. So after I saved it, I can press F5. And you can see here over in the console that it says RESTART, and then the script was run.

02:19 So let’s head over to the graphical user interface, and inside of copy_dir, I should now have a new folder, an empty folder called images/.

02:30 So this worked out just as expected, and my practice_files/ folder is still in its original state. Cool.

Become a Member to join the conversation.