Join two paths

EasyOS Module Programs · 10 of 49

Write a Python program to join two paths.

Advertisement
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

Leave a Comment