Split a path

EasyOS Module Programs · 43 of 49

Write a Python program to split a path into folder and file name.

Advertisement
Output
('reports\\2026', 'summary.txt')
('reports\\2026\\summary', '.txt')

Approach

  1. os.path.split returns (head, tail).

Solution

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

More os module programs

← All 49 OS Module Programs

Leave a Comment