Skip to main content
python remove directory

How To Delete a Directory In Python

We’ll learn how to delete a folder or directory in Python in this post. We’ll delete an empty folder as well as delete a directory and all files from it using shutil module.

Python shutil module

shutil module is a powerful module in Python that can be used for high-level file operations. Some of the operations that can be performed using shutil module are copying, moving and deleting files and directories. We’ll use os module along with shutil to perform these operations.

Deleting an empty directory or folder in Python is simple by using the os module.

We’ll use the following methods to delete a directory:

  • os.rmdir : Deletes a folder.
  • shutil.rmtree : Deletes a directory and all its contents.
  • pathlib.Path(empty_dir_path).rmdir() : The pathlib module was added in Python 3.4. This method is used to unlink and delete the empty folder.

Delete an Empty folder OR Directory

It’s important that the folder we’re going to delete is empty. A warning will appear, stating that the folder is not empty. We can determine the folder is empty or not using os.listdir() method.

folder_path = r"D:\python-test\logs"

if os.path.exists(folder_path):

    # checking whether the folder is empty or not
    if len(os.listdir(folder_path)) == 0:
        # removing the file using the os.rmdir() method
        os.rmdir(folder_path)
		print("Deleted '%s' directory successfully" % empty_dir)
    else:
        # messaging saying folder not empty
        print("Directory '%s' is not empty" % empty_dir)
else:
    print("The directory '%s'  not found" % empty_dir)

Error Handling while deleting a directory

We can also handle errors on the deletion of the directory using os.rmdir() method.

try:
    os.rmdir(path)
    print("Directory '% s' has been removed successfully" % directory)
except OSError as error:
    print(error)
    print("Directory '% s' can not be removed" % directory)

Output:

[WinError 145] The directory is not empty: 'D:/python-test/logs'
Directory 'logs' can not be removed

Delete a folder and all its subfolders recursively

The shutil module in Python allows you to conduct high-level operations on a file or a group of files. We’ll use shutil module’s .rmtree() method to remove delete a folder and all of the contents contained within it.

The .rmtree() function deletes the specified folder and all its subfolders recursively.

The Syntax:
shutil.rmtree(path, ignore_errors=False, onerror=None)

  • path – The path of the directory to delete. The symbolic links to a directory are not acceptable.
  • ignore_errors – If this flag is set to true, then the errors due to failed removals will be ignored.
  • onerror: If ignore_errors is false or omitted, such errors are handled by calling a handler specified by onerror.

Example: Delete directory Using rmtree()

import shutil

# Deleting an non-empty folder
dir_path = r"D:\python-test\logs"
shutil.rmtree(dir_path, ignore_errors=True)
print("Deleted '%s' directory successfully" % dir_path)

Output:

Deleted 'D:\python-test\logs' directory successfully

Unlink Folder using path.rmdir() method

You can also delete an empty directory by using the pathlib module’s rmdir() method. You first have to set the path for the directory, and then you call the rmdir() method on that path.

Syntax:

pathlib.Path(empty_dir_path).rmdir()

Parameter:

empty_dir_path: The empty directory path.

This method does not return any value.

Delete an empty directory

The pathlib module’s rmdir() method can also be used to remove or delete an empty directory.

import pathlib

# Deleting an empty folder
empty_dir = r"D:\python-test\logs"
path = pathlib.Path(empty_dir)
path.rmdir()
print("Deleted '%s' directory successfully" % empty_dir)

Output:

Deleted 'D:\python-test\logs' directory successfully

Leave a Reply

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