简体   繁体   中英

Why does Python's pathlib and os lib return different results for mapped network drives in Windows?

Trying to resolve a path using Python 3 on Windows 10. The path in question is available as a Mapped Network Drive. (This happens to be done through VirtualBox Shared Folders, but I hope that is not relevant here).

The good old os.path.abspath gives me a path that starts with the drive letter that was mapped in Windows. This is exactly what I expected and needed.

But when I try to upgrade to pathlib 's resolve function, I get a different result, in UNC notation. This is not expected, and not useful for my purposes. (Many programs do not accept UNC paths as input, they require a 'local' path.)

  1. What is the reason for this difference?
  2. How can one control this behavior so that pathlib can return a drive-letter-based path?
  3. Can anyone point me to documentation of this? I cannot find it in Python's documentation.

Demonstration:

PS C:\Users\user> python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import pathlib
>>> path = 'd:\\asdf'
>>> print(os.path.abspath(path))
d:\asdf
>>> print(pathlib.Path(path).resolve())
\\vboxsrv\code\asdf

I ran into this issue on Windows on parallels on a Mac. It's an ugly fix, but if you know the drive mapping ahead of time you can do something like:

filepath = pathlib.Path('.')
filepathstr = str(filepath.resolve()).replace('\\\\Mac\\Home', 'W:')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM