[Python-ideas] solution to cross-platform path handling problems

Andrew Barnert abarnert at yahoo.com
Sat Nov 23 21:20:55 CET 2013


On Nov 23, 2013, at 8:14, anatoly techtonik <techtonik at gmail.com> wrote:

> 1. All paths may have the mount point.
> 2. All paths without mount point are relative.
> 3. Default mount point for POSIX to make path absolute is '/'. Default
> mount point on Windows is current drive (e.g. 'c:/'), or UNC server
> address (e.g.\\host\).
> 4. Any (absolute?) path may be the mount point itself
> 5. path without mount point is called "relative"

The first problem with this is that there is already an established meaning for "mount point" in the POSIX world that this is very different from

Meanwhile, treating \\host\ as a root doesn't work. If you just skim the docs on UNC pathnames, or actually use them for anything, this is obvious. It's the \\host\share\ that's a root. \\host\ is not a usable path, and doesn't refer to anything path-like at the NT objects level, or the SMB/CIFS protocol. You can't .. above the share. You can't treat it as a drive. You can't mount it. (Yes, there are various places, especially in the msvcrt posix-like wrapper functions, where \\host\share\..\othershare works, but that's only because those functions are treating paths as plain strings and ignoring the semantics. The same functions also let you do \\host\..\otherhost\share, so if they imply that the host alone makes a path, they also imply that \\ alone makes a path, so the host still isn't a root.)

Also, this completely ignores the problem with pathlib that you're trying to solve: Windows paths don't come in just two forms, relative and absolute; they also have two intermediate forms, D:foo (which is relative to the current working directory on drive D rather than the current drive), and \foo (which is absolute on the current working drive). To handle these paths, you need to go beyond the notion of a current working directory and represent the notion Windows actually uses: a current working drive, and a current working directory on each drive. And to deal with the way cd'ing to a UNC path interacts with this... It's too complicated to spell out in one sentence, but if you read the MSDN docs they make it pretty clear. (Or just play around with Internet Explorer's file:// paths, which are Microsoft's answer to how to fit their filesystem into something cross-platform. If you think you can do better than them, you'll probably want to understand what they did and why.)


More information about the Python-ideas mailing list