Python
DOS path
path manipulation
programming
tutorial

How to split a dos path into its components in Python

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If you want to split a DOS or Windows path into its components in Python, use Windows-aware path tools instead of raw string splitting. The most reliable modern option is pathlib.PureWindowsPath, because it understands drive letters, UNC paths, roots, and final path parts even when your script runs on Linux or macOS.

The Cleanest Option: PureWindowsPath.parts

PureWindowsPath parses a Windows path using Windows rules without touching the real filesystem.

python
1from pathlib import PureWindowsPath
2
3path = PureWindowsPath(r"C:\Users\Admin\Documents\report.txt")
4
5print(path.parts)
6print(path.drive)
7print(path.root)
8print(path.name)

Typical output:

text
1('C:\\', 'Users', 'Admin', 'Documents', 'report.txt')
2C:
3\
4report.txt

This gives you the full component breakdown cleanly.

Useful Individual Properties

Besides .parts, you often want specific fields:

python
1from pathlib import PureWindowsPath
2
3path = PureWindowsPath(r"C:\Users\Admin\Documents\report.txt")
4
5print(path.parent)   # C:\Users\Admin\Documents
6print(path.stem)     # report
7print(path.suffix)   # .txt

That means you do not always need to split everything manually. Often the path API already exposes the exact component you want.

Parsing Windows Paths on Any OS

This is an important advantage of PureWindowsPath: it works correctly even if your current machine is not Windows.

python
1from pathlib import PureWindowsPath
2
3path = PureWindowsPath(r"D:\data\exports\daily.csv")
4print(path.parts)

If you used the platform-dependent Path on Linux for that same string, you could get behavior based on Linux path rules instead of Windows ones.

Older Alternative: ntpath

If you prefer the older os.path style API, use ntpath explicitly for DOS or Windows semantics.

python
1import ntpath
2
3path = r"C:\Users\Admin\Documents\report.txt"
4
5drive, rest = ntpath.splitdrive(path)
6head, tail = ntpath.split(path)
7
8print(drive)   # C:
9print(head)    # C:\Users\Admin\Documents
10print(tail)    # report.txt

You can apply ntpath.split repeatedly if you want all components, but PureWindowsPath.parts is usually clearer.

UNC Paths Also Work

Windows paths are not limited to drive letters. UNC paths need correct parsing too:

python
1from pathlib import PureWindowsPath
2
3unc = PureWindowsPath(r"\\server\share\folder\file.txt")
4
5print(unc.parts)
6print(unc.drive)
7print(unc.root)

UNC handling is one reason manual string splitting is fragile. The server and share part is not just an ordinary directory name. That distinction becomes important when network shares and local drive paths must be handled by the same codebase consistently every day.

Why Manual String Splitting Is Weak

You might be tempted to do this:

python
path = r"C:\Users\Admin\Documents\report.txt"
parts = path.split("\\")
print(parts)

That can work for simple cases, but it does not handle:

  • drive semantics cleanly
  • UNC paths
  • repeated separators
  • edge cases around roots

Path libraries already understand those rules, so use them.

A Small Helper Function

python
1from pathlib import PureWindowsPath
2
3def split_dos_path(path_text: str):
4    return PureWindowsPath(path_text).parts
5
6print(split_dos_path(r"C:\Temp\logs\app.log"))
7print(split_dos_path(r"\\server\share\folder\file.txt"))

Centralizing this in one helper can make cross-platform data-processing code much easier to test.

Common Pitfalls

One common mistake is using normal Python strings for Windows paths without escaping backslashes properly. Raw strings such as r"C:\Temp\file.txt" are usually clearer.

Another issue is using the current OS path module to parse Windows paths on a non-Windows machine. That can produce confusing results.

A third pitfall is forgetting that UNC paths and drive-letter paths follow slightly different rules. A good path library handles both.

Summary

  • Use PureWindowsPath(path).parts to split DOS or Windows paths reliably.
  • 'PureWindowsPath works on any operating system because it parses paths without filesystem access.'
  • Use .drive, .root, .name, and .parent when you need specific path components.
  • 'ntpath is a valid older alternative for Windows-style parsing.'
  • Avoid manual string splitting for anything beyond the simplest path strings.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.