[issue32442] Result of pathlib.Path.resolve() with UNC path is not very useful

Eryk Sun report at bugs.python.org
Thu Dec 28 23:14:39 EST 2017


Eryk Sun <eryksun at gmail.com> added the comment:

> the UNC path is not really useful anywhere in the Python 
> standard library 

UNC paths can be used almost anywhere in the file API. What specifically isn't working?

> there’s no way to turn the it (back) into network drive once you 
> call resolve()

Without using ctypes or PyWin32, you could resolve the root directory on drives A-Z to find the shortest matching path. This would also work with SUBST drives. For example:

    def resolve_mapped(path):
        path = pathlib.Path(path).resolve()
        mapped_paths = []
        for drive in 'ZYXWVUTSRQPONMLKJIHGFEDCBA':
            root = pathlib.Path('{}:/'.format(drive))
            try:
                mapped_paths.append(root / path.relative_to(root.resolve()))
            except (ValueError, OSError):
                pass
        return min(mapped_paths, key=lambda x: len(str(x)), default=path)

----------
nosy: +eryksun
type:  -> enhancement

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32442>
_______________________________________


More information about the Python-bugs-list mailing list