Delete File

Delete a file from disk using os.remove.

PythonBeginner
Python
# Program to delete a file

import os

filename = input("Enter filename to delete: ")

if os.path.exists(filename):
    os.remove(filename)
    print("Deleted", filename)
else:
    print("File does not exist.")

Output

Enter filename to delete: old.tmp
Deleted old.tmp

os.path.exists checks for the file first to avoid raising an exception when calling os.remove.