Python Practice › OS Module Programs › Program 20
Remove a file
Write a Python program to remove a file at a given path.
Output
False
Approach
os.remove(); check that it exists first.
Solution
import os open("old.log", "w").close()if os.path.exists("old.log"): os.remove("old.log")print(os.path.exists("old.log"))Output
False
Learn the concept
New to this topic? Read the lesson, then come back.