How To Delete A File In Python
Sep 17, 2021
The aim of this how-to-guide🏁 is to delete a file in python.
notes
import os
module- be explicit and include
try/except
block (EAFP style of exception handling) - use
os.remove(<filename>)
method
example
import os
myfile= raw_input("Enter file name to delete: ")
try:
os.remove(myfile)
except OSError as e:
print ("Error: %s - %s." % (e.filename, e.strerror))