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
os
moduleThe os
module contains the rmdir()
method that removes empty directories.
os.rmdir(path, *, dir_fd=None)
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.
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("."))
os
module.educative
directory by calling the mkdir()
method.listdir()
method of the os
module to list the contents of the present working directory.rmdir()
method to delete the educative
directory.educative
directory no longer exists in the current working directory.shutil
moduleThe shutil
module provides the rmtree()
method that removes the directory and all of its contents.
shutil.rmtree(path, ignore_errors=False, onerror=None)
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.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("."))
os
module.mkdir()
method to create the educative
directory.listdir()
method of the os
module to list the contents of the present working directory.rmtree()
method to delete the educative
directory.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.pathlib
moduleThe pathlib
module provides the rmdir()
method that removes empty directories.
Path.rmdir()
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("."))
os
module.mkdir()
method to create the educative
directory.listdir()
method of the os
module to list the contents of the present working directory.Path
object pointing to the educative
directory. The object is dir_file
.rmdir()
method on the dir_file
object to delete the educative
directory.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