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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Deleting a Directory

00:00 Okay, last exercise. Let’s look at that one. Delete the `my_folder/` directory. So the last task is to get rid of all the hard work that you just did and put your home folder back into its original state, which means we want to delete my_folder/ and anything that’s inside of my_folder/. So pathlib doesn’t actually allow you to do this.

00:22 One way to delete my_folder/ would be to go over all the files that are inside the my_folder/ directory or its subdirectory, images/, and first delete the files and then remove the empty folders.

00:36 But you can also use shutil to delete folders that have a content in them. Now, you want to be careful with this, right? BE CAREFUL!!

00:49 Because you don’t want to accidentally delete a folder that contains important stuff. So make sure that you’re deleting the right folders if you’re using this.

01:00 Okay, I’m going to import shutil. Then I want to use the .rmtree() method. That stands for remove tree. But before I do this, like I said, I want to be careful, so let’s double check that my_folder/ is actually what I expected it to be.

01:19 /Users/martin/my_folder. That path looks good. See what’s in there.

01:30 Okay, looks good. file2.txt—you know these are empty—some .DS_Store file that macOS creates, and an an images/ folder. Now, okay, so what’s inside of that images/ folder? Did I, did I maybe like accidentally move a really important image in there? Just to make sure, I want to also list everything that’s in there.

01:50 And for this, you can use a .rglob().

01:57 And then again, I’m passing the results, the object that gets generated from .rglob(), to the list() function so that we can see an output here. I have file2.txt, the .DS_Store file, and the folder images/, and it only contains one file that’s called image1.png.

02:16 Okay, that’s also just a thing that we created before. Now I feel confident that I can actually delete this directory. So I’m going to say shutil.rmtree(), and then pass in the path, which is my_folder.

02:35 And after this, my_folder should not exist anymore.

02:41 And it is gone, and with it, everything that was in there. Let’s check it out in the graphical user interface. So if I go back here to my home directory, you can see that the my_folder/ that used to hang out over here is gone. And my home directory is back to its original state.

Become a Member to join the conversation.