Python Practice › OS Module Programs › Program 10
Join two paths
Write a Python program to join two paths.
Output
reports\2026\summary.txt reports\2026\summary.txt
Approach
os.path.joinuses the right separator for the OS.
Solution
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.