Examples
- How to Create Directory in Python? New and If Not Exists
- How to Change Directory in Python? Change Current Working Directory
- How to Get Current Working Directory in Python?
- How to Remove Directory in Python? (Empty & With Files)
- How to Copy Directory in Python? Program With Examples
- How to Open Directory in Python? Program & Example
- How to Read All Files in Directory in Python? (All Types of Files)
- How to Copy File in Python From One Directory to Another? (With and Without shutil)
- Python File Append Program (Append to File)
- How to Read Text File in Python Line by Line? (3 Ways)
- How to Get File Name From File Path in Python?
- Get Number of Lines in File in Python (Line Count)
- How to Get Size of File in Python? (File Size Program)
How to Remove Directory in Python? (Empty & With Files)
Deleting or removing directories is a common task when working with file systems in Python. Sometimes, you may need to clean up your directory structure or remove unnecessary directories to manage your files efficiently. In this post, we will learn about different approaches to delete or remove directory in Python.
Python provides two main methods for deleting directories:
-
os.rmdir()
-
shutil.rmtree().
The os.rmdir() function is used to remove an empty directory, while the shutil.rmtree() function allows you to delete a directory and its contents recursively.
You will understand how to use both os.rmdir() and shutil.rmtree() functions with examples.
How to Remove Empty Directory in Python?
Here is an example to remove the empty directory using os.rmdir() function:
Code
import os
# Specify the directory path
directory = "my_directory"
# Remove the directory
os.rmdir(directory)
print("Directory deleted successfully!")
Output
Directory deleted successfully!
Explanation
In this program, the os.rmdir() function is used to remove the specified directory. The directory must be empty for os.rmdir() to work properly. If the directory contains any files or subdirectories, an error will occur.
Remove Directory in Python (With Files)
Following is the Python program to delete a directory along with its content or files:
Code
import shutil
# Specify the directory path
directory = "my_directory"
# Remove the directory and its contents recursively
shutil.rmtree(directory)
print("Directory deleted successfully!")
Output
Directory deleted successfully!
Explanation
In this program, the shutil.rmtree() function is used to delete the directory and its contents recursively. This function removes the specified directory and all its subdirectories and files, regardless of whether they are empty or not. Use this approach with caution, as it permanently deletes the directory and its contents.
After executing the respective function, if the directory is successfully deleted, the program will print "Directory deleted successfully!".
Note: Please note that directory deletion is a permanent action, and there is no built-in way to recover deleted files or directories. Therefore, exercise caution when deleting directories in your code, and ensure that you have appropriate backups or confirmation before performing deletion operations.