Creating a Testing Folder
00:00
I will again have to import shutil
, and then I can say shutil.copytree()
. And then here I also have the pop-up that tells me about the function signature. What I’m supposed to put in here is arguments.
00:17
First one is src
(source) and then dst
(destination). And the rest of the keyword parameters I’m going to ignore for now. So my source is going to be practice_dir
, and then as a destination, I need to give another path. Now, I haven’t defined the path yet, so maybe that would’ve been a good idea, but let’s just wing it here. And I’m going to say practice_dir.parent
.
00:40 I want to put it inside of the parent folder. And then I will combine this
00:48
with a new name. So I’m going to call it "copy_dir"
. Let’s see if that works. Okay, looks good. Like described in the documentation, I get the path of the new directory returned.
01:03 So now, if had I defined it before, then I could just use this right away. But for now, let’s remake the path here so that I can check whether it exists.
01:14
practice_dir.parent
, and then "copy_dir"
.
01:20
And then let’s see whether this actually got created. .exists()
. There you go. True
. So that looks good. One more checkup, let’s head over to the graphical user interface.
01:33
And there it is. Now I don’t only have practice_files/
, but also copy_dir/
. And looking in here, it seems that this copied everything.
01:47 Now I have two directories that contain the same file structure, which makes it easy for me to just do some tests on one of them without actually overwriting the file structure by moving the files in there.
01:59
And I also have a quick way to recreate that directory. So now I can do some operations. For example, I could say .copy_dir
. Well, let’s see. That was shutil.rmtree()
,
02:17
and I want to remove copy_dir
.
02:23
Now it should be gone. copy_dir.exists()
. It’s gone again. But now I have a quick way of re-creating it. I know I can just use shutil.copytree()
.
02:36
I want to copy my practice_dir
, and I want to create the copy_dir
.
02:43
Then copy_dir.exists()
. True
. All right, so now with this one line of code, or I guess two lines of code—I need to first define the path for the new directory and then also copy it, and I need to import shutil
. So in total, it’s three lines of code, but the first two, import shutil
and defining the path to the copy directory, I’m not going to have to run this again in this Shell session.
03:11
And I can do my operations, do my tests on copy_dir/
. I can go ahead and delete it and then just recreate it with one line of code. So that gives me this additional freedom to test frequently and just see what the script that I’m going to write over here is going to do to my file structure.
03:31 So the takeaway from this lesson is that it can be helpful to do a little bit of additional setup that gives you an opportunity to also test the code that you’re writing without altering the file structure that you need to run your script.
03:45 Now this is important for something like this for file system operations where you’re actually changing something that you’d need to re-create it, but it also applies for other code, where it’s often helpful to have some sort of a structure that you can easily re-create that allows you to run your code again, to figure out whether it does what you expected it to do.
04:04 So this was kind of a manual way to create a little bit of an opportunity for you to test more. And that’s it for this lesson. And in the next one, let’s start writing some code in the script.
Become a Member to join the conversation.