import os
"""
Deletes an empty folder at the given path.
Args:
folder_path (str): The path of the directory to be deleted.
Returns:
None
Raises:
OSError: If the folder is not empty or cannot be deleted.
"""
def delete_empty_folder(folder_path):
try:
os.rmdir(folder_path)
print(f"Folder '{folder_path}' was deleted successfully!")
except OSError as e:
print(f"Error while deleting the folder '{folder_path}': {e}")
folder_path = "d://data/" # example
delete_empty_folder(folder_path)
Example 2: Delete a file from a folder using os.remove()
import os
"""
Deletes a file from the given folder.
Args:
folder_path (str): The path of the folder containing the file.
file_name (str): The name of the file to be deleted.
Returns:
None
Raises:
OSError: If the file cannot be deleted.
"""
def delete_file_from_folder(folder_path, file_name):
try:
file_path = os.path.join(folder_path, file_name)
os.remove(file_path)
print(f"File '{file_name}' was deleted successfully from folder '{folder_path}'!")
except OSError as e:
print(f"Error while deleting file '{file_name}' from folder '{folder_path}': {e}")
folder_path = "d://data/"
file_name = "data_2023.csv"
delete_file_from_folder(folder_path, file_name)
Example 3: Delete a folder and all its files using shutil.rmtree()
import shutil
"""
Deletes a folder and all its contents.
Args:
folder_path (str): The path of the folder to be deleted.
Returns:
None
Raises:
FileNotFoundError: If the folder does not exist.
"""
def delete_folder(folder_path):
try:
shutil.rmtree(folder_path)
print(f"Folder '{folder_path}' and its contents were deleted successfully!")
except FileNotFoundError as e:
print(f"Error while deleting folder '{folder_path}': {e}")
# Example usage
folder_path = "d://data/"
delete_folder(folder_path)

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python-Programs,
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- 25: How to rename a file using Python Program
- 22: Send Yahoo! Email using smtplib - SMTP protocol client using Python Program
- Program 15: Find String is a Palindrome or Not - 1000+ Python Programs
- 21: Program to Delete File or Folder in Python
- Program 14: Sum of Even Numbers from 1 to 100 - 1000+ Python Programs
- Program 2: Print your name using print() function - 1000+ Python Programs
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- 16: Find the largest element in a List - 1000+ Python Programs
- 33: Python Program to find the current time in India (IST)
- 23: Python Programs to concatenate two Lists
- 27: Measure Elapsed Time for a Python Program Execution
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- Program 10: Modulo of Two Numbers - 1000+ Python Programs
- Program 1: Print Hello World! - 1000+ Python Programs
- Program 11: Calculate Percentage - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- 17: Find Factorial of a Number - 1000+ Python Programs
- 35: Python Program to find the System Hostname
- 19: Simple Calculator using built-in functions - 1000+ Python Programs
- 34: Traverse a List in Reverse Order - 1000+ Python Programming
- Program 3: Print the name of user as input - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
More Posts:
- How to come out of dquote prompt in Terminal - macOS/Linux - MacOS
- Docker - Error response from daemon: You cannot remove a running container - Docker
- Create Custom Toast Android Programming - Android
- How to Add Developer Tab in Excel for Mac - Microsoft
- The declared package does not match the expected package Eclipse - Android
- AlertDialog with single button example : Android - Android
- How to Convert Jupyter Notebook and Save as PDF - Python
- TextEdit: Disable Autocorrect Option (Mac) - MacOS