[英]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.尝试在 Windows 10 上使用 Python 3 解析路径。有问题的路径可用作映射网络驱动器。 (This happens to be done through VirtualBox Shared Folders, but I hope that is not relevant here). (这恰好是通过 VirtualBox 共享文件夹完成的,但我希望这与此处无关)。
The good old os.path.abspath
gives me a path that starts with the drive letter that was mapped in Windows.好的旧os.path.abspath
为我提供了一个以在 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.但是当我尝试升级到pathlib
的resolve
函数时,我得到了不同的结果,用 UNC 表示法。 This is not expected, and not useful for my purposes.这不是预期的,对我的目的没有用。 (Many programs do not accept UNC paths as input, they require a 'local' path.) (许多程序不接受 UNC 路径作为输入,它们需要“本地”路径。)
pathlib
can return a drive-letter-based path?如何控制这种行为,以便pathlib
可以返回基于驱动器号的路径?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.我在 Mac 上的并行 Windows 上遇到了这个问题。 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:')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.