Python Practice › OS Module Programs › Program 7
Create 10 nested folders
Write a Python program to create 10 directories nested inside each other.
Output
10 True
Approach
- Join 10 names into one path and call
os.makedirsonce.
Solution
import os path = os.path.join("nested", *[f"level{i}" for i in range(1, 11)])os.makedirs(path, exist_ok=True)print(path.count(os.sep), os.path.isdir(path))Output
10 True
Line by line
Line 3
* passes the list of names as separate arguments to os.path.join.
Learn the concept
New to this topic? Read the lesson, then come back.