Python Practice › OS Module Programs › Program 11
Is there a file at a path?
Write a Python program to check whether a file exists at a given path.
Output
sample.txt True missing.txt False . False
Approach
os.path.isfile()is True only for existing files.
Solution
import os open("sample.txt", "w").close()for p in ["sample.txt", "missing.txt", "."]: print(p, os.path.isfile(p))Output
sample.txt True missing.txt False . False
Learn the concept
New to this topic? Read the lesson, then come back.