Python

Python delete directory

Python offers built-in modules and functions that help you to delete directories and files. Removing files or directories is a critical operation in which you cannot easily retrieve the directory content once you deleted the directory. Therefore, using some helpful python functions, users can easily remove the directory and its content.

In this article, we will demonstrate different methods through which you can delete a directory in python. We will mention some examples that will help you to understand the directory delete python functions.

Python Functions to remove directory

There are following three different functions are available in python to remove a directory or folder:

Remove directory using os.rmdir() function

The os.rmdir() function is used to remove the empty directory in python. In this case, the required directory to be empty; otherwise, it will raise an OSError. The FileNOtFoundError is raised if the directory does not exist.

Example

The following source code is an example of os.rmdir() function:

import os
path_dir = 'C:\\Users\DELL\Desktop\empty_folder'
try:
    os.rmdir(path_dir)
    print("directory deleted")
except OSError as e:
    print("Error: %s : %s" % (path_dir, e.strerror))

The path of the directory that you want to remove will save into the ‘path_dir’ variable. The following output shows on the window:

Remove directory using pathlib module

By including the pathlib module, the Path.rmdir() function also helps to delete an empty directory.

Example

The below-given example will show the demonstration of pathlib module and Path.rmdir() function:

from pathlib import Path
path_dir = Path('C:\\Users\DELL\Desktop\empty_folder')
try:
    path_dir.rmdir()
    print("Directory removed successfully")
except OSError as e:
    print("Error: %s : %s" % (path_dir, e.strerror))

The path_dir variable keeps the directory’s path that you want to remove. After running the above source code, the following output will show on the window:

Remove directory using shutil.rmtree()

Using the shutil.rmtree() function, you can delete or remove the required directory, including its contents, from your system. So, use the shutil module to remove a directory tree.

Example

The following example will show you how to remove a directory using the shutil python module:

import shutil
path_dir = 'C:\\Users\DELL\Desktop\personal_folder'
try:
    shutil.rmtree(path_dir)
    print("directory removed successfully")
except OSError as e:
    print("Error: %s : %s" % (path_dir, e.strerror))

The below-given results show on the source code window after executing the above python code:

Conclusion

We have described in this article how to delete a directory with the help of different python functions. We have mentioned three functions os.rmdir(), pathlib module, and shutil.rmtree(), through which you can easily remove a directory in python. I hope these functions will help you to delete directories from your system. However, be careful while removing a directory and its content because once the directory is deleted, you can’t recover it easily.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.