You just learned how to delete files in Python with os.remove(). But calling os.remove() on a file that does not exist raises a FileNotFoundError and crashes your script.
Your job is to delete a file safely — only if it actually exists — so the script never crashes, no matter what.
A file named report.txt has been created for you before your code runs. Delete it safely.
Your tasks
1. Import the os module
2. Set a variable filename = "report.txt"
3. Use os.path.exists() to check if the file exists. If it does, call os.remove() to delete it and print "File deleted." — otherwise print "File not found — nothing to delete."
Expected output
File deleted.
Constraints
- Use
os.path.exists() to check the file before deleting
- Use
os.remove() to delete the file
- Do not use
try/except — this exercise is about os.path.exists()
- No external libraries beyond
os