import os data_file = "data1.txt" os.remove(data_file) import os data_file = "data2.txt" os.unlink(data_file) import os data_file = "home/data.txt" # If the file exists, delete it if os.path.isfile(data_file): os.remove(data_file) else: print(f"Error: {data_file} not a valid filename") import os data_file = "home/data.txt" # Use exception handling try: os.remove(data_file) except OSError as e: print(f"Error: {data_file} : {e.strerror}") from pathlib import Path data_file = Path("home/data.txt") try: data_file.unlink() except IsADirectoryError as e: print(f"Error: {data_file} : {e.strerror}")