Python Practice › OS Module Programs › Program 46
Does a path exist?
Write a Python program to check whether a path exists.
Output
here.txt True . True nowhere False
Approach
os.path.existsworks for files and folders.
Solution
import os open("here.txt", "w").close()for p in ["here.txt", ".", "nowhere"]: print(p, os.path.exists(p))Output
here.txt True . True nowhere False
Learn the concept
New to this topic? Read the lesson, then come back.