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:

  • os
  • shutil
  • pathlib

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 os
print("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 os module.
  • Line 4: We create the educative directory by calling the mkdir() method.
  • Line 7: We use the listdir() method of the os module to list the contents of the present working directory.
  • Line 9: We call the rmdir() method to delete the educative directory.
  • Line 11: We print the contents of the present working directory. We can observe that the educative directory 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 is true, 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 set ignore_errors to true to invoke the handler.

Example

import shutil, os
print("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 os module.
  • Line 4: We call the mkdir() method to create the educative directory.
  • Line 7: We use the listdir() method of the os module to list the contents of the present working directory.
  • Line 9: We call the rmtree() method to delete the educative directory.
  • Line 11: We use the listdir() method of the os module to list the contents of the present working directory. We can observe that the educative directory 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, os
print("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 os module.
  • Line 4: We call the mkdir() method to create the educative directory.
  • Line 7: We use the listdir() method of the os module to list the contents of the present working directory.
  • Line 9: We create a Path object pointing to the educative directory. The object is dir_file.
  • Line 10: We call the rmdir() method on the dir_file object to delete the educative directory.
  • Line 12: We use the listdir() method of the os module to list the contents of the present working directory. We can observe that the educative directory no longer exists in the current working directory.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved