This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Result of pathlib Path.resolve() with UNC path is not very useful
Type: enhancement Stage: resolved
Components: Library (Lib), Windows Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, eryksun, ikelos, paul.moore, steve.dower, tim.golden, uranusjr, zach.ware
Priority: normal Keywords:

Created on 2017-12-28 19:09 by uranusjr, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg309137 - (view) Author: Tzu-ping Chung (uranusjr) * Date: 2017-12-28 19:09
The behaviour of os.path.abspath() and pathlib.Path.resolve() is a different when dealing with a path on a UNC share (on Windows):

    >>> import os, pathlib
    >>> path = pathlib.Path('Z:\foo')
    >>> path.resolve()
    WindowsPath('//host/share/foo')
    >>> os.path.abspath(path)
    'Z:\\foo'

This is not necessarily a problem by itself, just a consequence of calling different APIs underneath (although it probably worths a mention in documentation). The real problem is that the UNC path is not really useful anywhere in the Python standard library (AFAIK), and there’s no way to turn the it (back) into network drive once you call resolve(). The only way to get a network drive path is to

    >>> pathlib.Path(os.path.abspath(path))

Some possibile solutions:

1. Change the behaviour of resolve() to return the network drive path instead. This would be the most straightforward, API-wise, but is backward-incompatible, and maybe the original implementation did this on purpose?
2. Add a as_absolute() to pathlib.Path. On Windows this would mirror the result of os.path.abspath(); on POSIX this would probably be identical to resolve().
3. Add an argument to resolve()? This is essentially the same as 2., just interfaced differently.
msg309148 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2017-12-28 23:20
I'm not sure how pathlib.Path('Z:\foo') gives an answer with 'foo' in it, since '\f' is a formfeed. Is this the exact output that you're showing?

Can you try with r'Z:\foo', 'Z:\\foo', or 'Z:/foo' and see what that produces?
msg309160 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-12-29 04:14
> 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)
msg309162 - (view) Author: Tzu-ping Chung (uranusjr) * Date: 2017-12-29 06:23
@Eric

You’re correct, this is not the exact output. I tested the commands with a different path, but didn’t format them correctly when I convert them to use a generic example. Sorry for the confusion.


@Eryk

I found my problem. I was doing some os.rename and shutil stuff that constantly fails when moving and copying things using UNC paths, but works flawlessly when I convert them to use drives. I assumed it’s because the UNC, but after a more involved debugging I find all UNC calls work by themselves, only fail when I combine them. I guess it is due to some strange Windows thing, maybe UNC file stat can’t update fast enough for the next call to get the correct state or something.

The attached recipe worked very well. Thanks for the help!

I’m closing this since the problem is likely not Python-related. Sorry for the disturbance.
msg412601 - (view) Author: Mike Auty (ikelos) Date: 2022-02-05 22:21
Sorry for the spam, thought I was in a different text box.  5:(
History
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76623
2022-02-05 22:21:46ikelossetnosy: + ikelos

messages: + msg412601
title: file_open unc -> Result of pathlib Path.resolve() with UNC path is not very useful
2022-02-05 22:20:45ikelossettitle: Result of pathlib.Path.resolve() with UNC path is not very useful -> file_open unc
2017-12-29 06:23:34uranusjrsetstatus: open -> closed
resolution: not a bug
messages: + msg309162

stage: resolved
2017-12-29 04:14:39eryksunsettype: enhancement

messages: + msg309160
nosy: + eryksun
2017-12-28 23:20:05eric.smithsetnosy: + eric.smith
messages: + msg309148
2017-12-28 19:09:57uranusjrcreate