Hi all,
A quick rundown of some notable feature differences and duplications between pathlib and os.path, also showing changes in the last year or so.
Path.expanduser() and os.path.expanduser() [complete!]
These implementations were almost identical, save for some subtleties in how Windows home directories are guessed.
Addressed in PR 18841, which deleted pathlib’s implementation and made it call os.path.expanduser()
.
Path.resolve() and os.path.realpath() [complete!]
Only resolve()
was capable of throwing exceptions when missing files or symlink loops were encountered, whereas realpath()
always appended the remaining path segment and returned without indicating an error.
This was addressed in PR 25264, which added a strict
argument to realpath()
, deleted pathlib’s own implementation and made it call realpath()
PurePath() and os.path.normpath() [pr available!]
PurePath
automatically applies safe normalization to paths, e.g. redundant separators and .
entries are removed. It does not collapse ..
entries, as doing so cannot be done safely unless we also resolve symlinks along the way, which requires filesystem access.
Path
objects provide a resolve()
method that will safely resolve symlinks and ..
entries simultaneously.
On the other hand, os.path.normpath()
always naively collapses ..
entries, which can change the meaning of paths involving symlinks. There’s no equivalent to PurePath
’s normalization.
I’ve opened PR 26694 to add a strict
argument to normpath()
.
PurePath.is_reserved() and os.path.??? [todo!]
There’s no equivalent to pathlib’s PurePath.is_reserved()
in os.path
. For full parity this should be added.
PurePath.as_uri() and os.path.??? [todo!]
There’s no equivalent to pathlib’s PurePath.as_uri()
in os.path
. For full parity this should be added.
… and I think that’s everything!.
With these changes in place, pathlib’s _Flavour
abstraction is entirely vestigial and can be safely removed. By moving the OS-specific bits into the low-level ntpath
+ posixpath
modules, we free pathlib from the burden of re-implementing OS path quirks. That in turn allows for some careful refactoring as proposed by @kfollstad here:
Any feedback/questions/concerns very welcome! Thanks for reading.
Cheers