Create 10 nested folders

EasyOS Module Programs · 7 of 49

Write a Python program to create 10 directories nested inside each other.

Advertisement
Output
10 True

Approach

  1. Join 10 names into one path and call os.makedirs once.

Solution

Python
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.

More os module programs

← All 49 OS Module Programs

Leave a Comment