List everything in a folder

List everything in a folder

EasyOS Module Programs · 13 of 49

Write a Python program to list all the files and folders in a target path.

Output
['a.txt', 'b.csv', 'sub']

Approach

  1. os.listdir() returns names; sort them for a stable order.

Solution

Python
import os os.makedirs("target/sub", exist_ok=True)for name in ["a.txt", "b.csv"]:    open(os.path.join("target", name), "w").close()print(sorted(os.listdir("target")))
Output
['a.txt', 'b.csv', 'sub']

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Count files in a folder

Count files in a folder

EasyOS Module Programs · 14 of 49

Write a Python program to count the files in a target path.

Output
3

Approach

  1. Count entries that are files.

Solution

Python
import os os.makedirs("target/sub", exist_ok=True)for name in ["a.txt", "b.csv", "c.txt"]:    open(os.path.join("target", name), "w").close()count = sum(1 for n in os.listdir("target") if os.path.isfile(os.path.join("target", n)))print(count)
Output
3

Line by line

Line 6

listdir gives bare names, so each one is joined with the folder before checking.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Count folders in a folder

Count folders in a folder

EasyOS Module Programs · 15 of 49

Write a Python program to count the folders in a target path.

Output
3

Approach

  1. os.scandir gives entries with is_dir().

Solution

Python
import os for d in ["one", "two", "three"]:    os.makedirs(os.path.join("target", d), exist_ok=True)open(os.path.join("target", "file.txt"), "w").close()with os.scandir("target") as entries:    print(sum(1 for e in entries if e.is_dir()))
Output
3

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Is there a folder at a path?

Is there a folder at a path?

EasyOS Module Programs · 12 of 49

Write a Python program to check whether a directory exists at a given path.

Output
data True
nowhere False
data/x.txt False

Approach

  1. os.path.isdir().

Solution

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

More os module programs

← All 49 OS Module Programs

Remove a folder that is not empty

Remove a folder that is not empty

EasyOS Module Programs · 9 of 49

Write a Python program to remove a directory that contains files.

Output
False

Approach

  1. shutil.rmtree() deletes a folder and everything inside it.

Solution

Python
import osimport shutil os.makedirs("full_dir/sub", exist_ok=True)open("full_dir/sub/a.txt", "w").close()shutil.rmtree("full_dir")print(os.path.exists("full_dir"))
Output
False
Watch out

rmtree deletes permanently. Double-check the path before calling it.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Join two paths

Join two paths

EasyOS Module Programs · 10 of 49

Write a Python program to join two paths.

Output
reports\2026\summary.txt
reports\2026\summary.txt

Approach

  1. os.path.join uses the right separator for the OS.

Solution

Python
import osfrom pathlib import Path print(os.path.join("reports", "2026", "summary.txt"))print(Path("reports") / "2026" / "summary.txt")
Output
reports\2026\summary.txt
reports\2026\summary.txt
Note

The output shows Windows separators; on macOS and Linux you would see /.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Is there a file at a path?

Is there a file at a path?

EasyOS Module Programs · 11 of 49

Write a Python program to check whether a file exists at a given path.

Output
sample.txt True
missing.txt False
. False

Approach

  1. os.path.isfile() is True only for existing files.

Solution

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

More os module programs

← All 49 OS Module Programs

Create 10 nested folders

Create 10 nested folders

EasyOS Module Programs · 7 of 49

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

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

Remove an empty folder

Remove an empty folder

EasyOS Module Programs · 8 of 49

Write a Python program to remove an empty directory with os.rmdir().

Output
False

Approach

  1. rmdir only removes empty folders.

Solution

Python
import os os.makedirs("empty_dir", exist_ok=True)os.rmdir("empty_dir")print(os.path.exists("empty_dir"))
Output
False

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs

Create 10 folders with random names

Create 10 folders with random names

EasyOS Module Programs · 6 of 49

Write a Python program to create 10 directories with random names.

Output
10 ['dissyk', 'dwtgml', 'fkaflm']

Approach

  1. Build random names with random.choices and create each folder.

Solution

Python
import osimport randomimport string random.seed(1)os.makedirs("random_dirs", exist_ok=True)for _ in range(10):    name = "".join(random.choices(string.ascii_lowercase, k=6))    os.makedirs(os.path.join("random_dirs", name), exist_ok=True)print(len(os.listdir("random_dirs")), sorted(os.listdir("random_dirs"))[:3])
Output
10 ['dissyk', 'dwtgml', 'fkaflm']

Line by line

Line 5

The seed makes the names repeatable; remove it for new names each time.

Learn the concept

New to this topic? Read the lesson, then come back.

More os module programs

← All 49 OS Module Programs