How to remove a directory with Python

When one wants to script their daily computer tasks with the main purpose of automating their stuff, there is a high chance they have to deal with the file system of their operating system, and for sure computer directories. The Python computer programming language, comes equipped with all the utilities the coder needs to interact with the functionalities provided by their operating system.

There are many builtin utilities one can make use of to remove a directory in their operating system. With the main purpose of keeping stuff practical, I highly recommend that you open a new Python interactive console before going any further with this tutorial.

Once you have opened your Python interactive shell, make sure to import the rmdir utility like shown in the following example.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

from os import rmdir

Then create a new empty directory through the graphical user interface provided by your operating system. Personally, I am creating a new empty directory in my computer Desktop. The new directory is being called NEW_DIRECTORY.

With the help of the rmdir utility, the Python coder can remove their empty directory by providing its absolute path as an argument, like shown in the following example.

rmdir(‘/User/oltjano/Desktop/NEW_DIRECTORY’)

When one tries to remove a directory with files in it by using the rmdir utility, they will fail. According to the Python official documentation, the rmdir utility fails to remove directories which have files within them.

To prove it to yourself that such claim is true, make sure to run the rmdir utility with the absolute path of a directory which has files in it like shown in the following example.

your_path = ‘/Users/oltjano/Desktop/Coder’ # the path of the directory
os.rmdir(your_path)

Once the above piece of Python code has been executed on my Python interactive console, I get the following error. It is an OSError.

Traceback (most recent call last):
File “”, line 1, in
OSError: [Errno 66] Directory not empty: ‘/Users/oltjano/Desktop/Coder’

The above error is suggesting that the rmdir can not remove a directory which has files within it.

How can the Python coder remove a directory which is not empty? It is not that hard! There is another Python module called shutil one needs to import. It provides a utility called rmtree, which is able to remove the directory and its entire content.

First import the shutil module like shown below.

import shutil

Then make use of the rmtree utility that the shutil module provides like shown in the following practical example.

# your_path is the absolute path of the directory
shutil.rmtree(your_path)

After the above Python command got executed in my interactive shell, no error was thrown. It probably means that the directory and all its contents are finally being removed.

Final Thoughts

One can easily make use of the os.rmdir utility to remove an empty directory, but unfortunately it does not work with directories that have content in them. Being rich in features, the Python computer programming language comes shipped with shutil.rmtree by default, a function which can remove a directory and its entire tree, its entire contents.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *