Python Practice › OS Module Programs › Program 43
Split a path
Write a Python program to split a path into folder and file name.
Output
('reports\\2026', 'summary.txt')
('reports\\2026\\summary', '.txt')Approach
os.path.splitreturns (head, tail).
Solution
import os path = os.path.join("reports", "2026", "summary.txt")print(os.path.split(path))print(os.path.splitext(path))Output
('reports\\2026', 'summary.txt')
('reports\\2026\\summary', '.txt')Learn the concept
New to this topic? Read the lesson, then come back.