What are different ways to delete a directory in Python?
Overview
There are multiple ways and different functions to delete a directory in Python.
In this Answer, we cover functions available to delete a directory in the following modules:
osshutilpathlib
The os module
The os module contains the rmdir() method that removes empty directories.
Syntax
os.rmdir(path, *, dir_fd=None)
Parameters
path: This is the directory path.dir_fd: This is the directory file descriptor.
If the directory (to be deleted) contains files, we can traverse the contents of the directory using the os.listdir() method. This removes each of the directory contents. Then, rmdir() can be used to delete the empty directory.
Example
import osprint("Creating directory 'educative' ...")os.mkdir("educative")print("Directory created")print("Contents of the present working directory")print(os.listdir("."))print("Deleting directory 'educative' ...")os.rmdir("educative")print("Contents of the present working directory")print(os.listdir("."))
Explanation
- Line 1: We import the
osmodule. - Line 4: We create the
educativedirectory by calling themkdir()method. - Line 7: We use the
listdir()method of theosmodule to list the contents of the present working directory. - Line 9: We call the
rmdir()method to delete theeducativedirectory. - Line 11: We print the contents of the present working directory. We can observe that the
educativedirectory no longer exists in the current working directory.
The shutil module
The shutil module provides the rmtree() method that removes the directory and all of its contents.
Syntax
shutil.rmtree(path, ignore_errors=False, onerror=None)
Parameters
path: This is the directory path.ignore_errors: This is a boolean value. If it istrue, the errors from failed removals will be ignored.onerror: This is a handler function that is invoked when errors are seen during the removal process. We setignore_errorstotrueto invoke the handler.
Example
import shutil, osprint("Creating directory 'educative' ...")os.mkdir("educative")print("Directory created")print("Contents of the present working directory")print(os.listdir("."))print("Deleting directory 'educative' ...")shutil.rmtree("educative")print("Contents of the present working directory")print(os.listdir("."))
Explanation
- Line 1: We import the
osmodule. - Line 4: We call the
mkdir()method to create theeducativedirectory. - Line 7: We use the
listdir()method of theosmodule to list the contents of the present working directory. - Line 9: We call the
rmtree()method to delete theeducativedirectory. - Line 11: We use the
listdir()method of theosmodule to list the contents of the present working directory. We can observe that theeducativedirectory no longer exists in the current working directory.
The pathlib module
The pathlib module provides the rmdir() method that removes empty directories.
Syntax
Path.rmdir()
Example
import pathlib, osprint("Creating directory 'educative' ...")os.mkdir("educative")print("Directory created")print("Contents of the present working directory")print(os.listdir("."))print("Deleting directory 'educative' ...")dir_file = pathlib.Path("educative/")dir_file.rmdir()print("Contents of the present working directory")print(os.listdir("."))
Explanation
- Line 1: We import the
osmodule. - Line 4: We call the
mkdir()method to create theeducativedirectory. - Line 7: We use the
listdir()method of theosmodule to list the contents of the present working directory. - Line 9: We create a
Pathobject pointing to theeducativedirectory. The object isdir_file. - Line 10: We call the
rmdir()method on thedir_fileobject to delete theeducativedirectory. - Line 12: We use the
listdir()method of theosmodule to list the contents of the present working directory. We can observe that theeducativedirectory no longer exists in the current working directory.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved