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.
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.
Typical output:
This gives you the full component breakdown cleanly.
Useful Individual Properties
Besides .parts, you often want specific fields:
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.
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.
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:
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:
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
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).partsto split DOS or Windows paths reliably. - '
PureWindowsPathworks on any operating system because it parses paths without filesystem access.' - Use
.drive,.root,.name, and.parentwhen you need specific path components. - '
ntpathis a valid older alternative for Windows-style parsing.' - Avoid manual string splitting for anything beyond the simplest path strings.
Related reading
- how to split an iterable in constant-size chunks
- How to split by comma and strip white spaces in Python?
- How to split data based on a column value in sklearn
- How to split text without spaces into list of words
- How to state in requirements.txt a direct github source
- How to step through Python code to help debug issues?
- How to stop a looping thread in Python?
- How to stop Python Kafka Consumer in program?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.