import os # Path to the file to be deleted file_path = "path/to/your/file.txt"
def delete_file(file_path): try: # Delete the file os.remove(file_path) print(f"File '{file_path}' deleted successfully.") except Exception as e: print(f"Failed to delete file '{file_path}': {e}")
# Call the function to delete the file delete_file(file_path)
The file with path path/to/your/file.txt will be deleted.
Replace "path/to/your/file.txt"
with the actual path to the file you want to delete. This script attempts to delete the specified file using os.remove()
. 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.