import os import shutil # Path to the directory to be deleted dir_path = "path/to/your/directory"
def delete_directory(dir_path): try: # Delete the directory and its contents shutil.rmtree(dir_path) print(f"Directory '{dir_path}' deleted successfully.") except Exception as e: print(f"Failed to delete directory '{dir_path}': {e}")
# Call the function to delete the directory delete_directory(dir_path)
The directory path/to/your/directory will be deleted.
Replace "path/to/your/directory"
with the actual path to the directory you want to delete. This script attempts to delete the specified directory and all its contents using shutil.rmtree()
. If successful, it prints a success message; otherwise, it prints an error message.
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.