Sub topic 13
Handling Windows and POSIX paths on any system
This chapter assumes you know Path objects, the / operator, and the path attributes name, stem, suffix, and parent. It also assumes you know the difference between pure and concrete paths from earlier chapters.
Why a path flavor matters
Windows and POSIX systems (Linux, macOS) write paths differently.
| Feature | POSIX | Windows |
|---|---|---|
| Separator | `/` | `\` (and `/` is accepted) |
| Drive letter | none | `C:` |
| Case sensitivity | usually case-sensitive | usually case-insensitive |
Path("x") picks the flavor of the system your program runs on. Sometimes you need the other flavor. You might read a Windows path from a log file on a Linux server, or write a test that must behave the same on every machine. Pure path classes handle both cases.
The class family
Pure classes only parse and manipulate path text. They never touch the file system, so you can use either flavor on any operating system.
The concrete classes PosixPath and WindowsPath add methods that use the file system, such as exists() and read_text(). Path(...) returns whichever one matches your system. Creating the other concrete class on your system raises an error, because it could not work there.
Parsing a Windows path on Linux
from pathlib import PureWindowsPath
w = PureWindowsPath("C:/Users/ana/file.txt")
print(w.name) # file.txt
print(w.drive) # C:
print(w.parent) # C:\Users\anaThis runs the same way on Linux, macOS, and Windows. A plain Path on Linux would treat the backslashes in C:\Users\ana\file.txt as ordinary characters in one long file name.
Converting between flavors
Use as_posix() to get a string with forward slashes. Use .parts to move the pieces into another flavor.
from pathlib import PurePosixPath, PureWindowsPath
win = PureWindowsPath(r"C:\Users\ana\docs\report.txt")
print(win.as_posix()) # C:/Users/ana/docs/report.txt
# Keep everything after the drive and root, and rebuild it as a POSIX path.
relative_parts = win.parts[1:] # ('Users', 'ana', 'docs', 'report.txt')
linux = PurePosixPath("/srv/uploads", *relative_parts)
print(linux) # /srv/uploads/Users/ana/docs/report.txtwin.parts[0] is 'C:\\', the drive plus root. Dropping it and keeping the rest gives you a relative path you can attach to any base directory.
Comparison rules follow the flavor
Windows paths compare without regard to case. POSIX paths do not.
from pathlib import PurePosixPath, PureWindowsPath
print(PureWindowsPath("Docs/A.TXT") == PureWindowsPath("docs/a.txt")) # True
print(PurePosixPath("Docs/A.TXT") == PurePosixPath("docs/a.txt")) # FalseChoose the flavor that matches the system the path came from. Pick the wrong one and equality checks, sorting, and in tests on sets of paths can give the wrong answer.
Pure paths cannot query the disk
A pure path has no exists(), is_file(), read_text(), or mkdir(). Calling one raises AttributeError. This is intentional. A Windows path from a log has no meaning on your Linux disk, so pathlib refuses to check it. If you need to access the disk, convert the pure path to a Path first.
When to use which class
- Use
Pathfor everything that reads or writes files on the current machine. - Use
PureWindowsPathorPurePosixPathwhen the path comes from another system or is only data, such as a log line, an archive entry, or a configuration value. - Use a pure class in tests when the result must not depend on the operating system running the tests.
Exercise
A Linux server reads the string D:\Backups\2026\db.sql from a log. Write code that prints the file's name and its suffix without using Path. Why does Path give the wrong answer here?
Show answer
Use PureWindowsPath: p = PureWindowsPath(r"D:\Backups\2026\db.sql"); print(p.name, p.suffix) prints db.sql .sql. On Linux, Path is a PosixPath, which treats \ as an ordinary character. The whole string is then one file name, and name would return it unchanged.
Exercise
Given PureWindowsPath("C:/Data/Report.CSV"), what does comparing it with PureWindowsPath("c:/data/report.csv") return, and what changes if you use PurePosixPath for both?
Show answer
With PureWindowsPath the comparison returns True, because Windows paths ignore case. With PurePosixPath it returns False, because POSIX paths are case-sensitive.