Python Practice › OS Module Programs › Program 12
Is there a folder at a path?
Write a Python program to check whether a directory exists at a given path.
Output
data True nowhere False data/x.txt False
Approach
os.path.isdir().
Solution
import os os.makedirs("data", exist_ok=True)for p in ["data", "nowhere", "data/x.txt"]: print(p, os.path.isdir(p))Output
data True nowhere False data/x.txt False
Learn the concept
New to this topic? Read the lesson, then come back.