简体   繁体   English

为什么 Python 的 pathlib 和 os lib 为 Windows 中的映射网络驱动器返回不同的结果?

[英]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.但是当我尝试升级到pathlibresolve函数时,我得到了不同的结果,用 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 路径作为输入,它们需要“本地”路径。)

  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?如何控制这种行为,以便pathlib可以返回基于驱动器号的路径?
  3. Can anyone point me to documentation of this?任何人都可以指出我的文档吗? I cannot find it in Python's documentation.我在 Python 的文档中找不到它。

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.

相关问题 为什么 python pathlib Path('').exists() 返回 True? - Why does the python pathlib Path('').exists() return True? 为什么 pathlib.Path(".").parent 不返回“..”? - Why does pathlib.Path(".").parent not return ".."? Python os.path.isfile抱怨映射网络驱动器中的文件不存在 - Python os.path.isfile complains that file in mapped network drive does not exist 为什么 pathlib.Path(“C:”) 解析为 Windows 上的工作目录? - Why does pathlib.Path(“C:”) resolve to the working directory on Windows? 为什么 lambda 指令返回不同的结果? - Why does the lambda instruction return different results? 为什么python os.path.isfile在Windows上仅针对特定目录返回false? - Why does python os.path.isfile return false on windows, for only a specific directory? Python-Os.walk遍历不同驱动器中的目录列表 - Python - Os.walk loop through list of directories in different drives 在Python中,如何在Windows中将本地硬盘与网络和软盘分开? - in Python, how to separate Local Hard Drives from Network and Floppy in Windows? 为什么 Python pathlib relative_to 允许多个输入路径? - Why does Python pathlib relative_to allow multiple input paths? 为什么Windows XP上Python的os.system()命令在开始运行时需要两个双引号? - Why does Python's os.system() command on Windows XP require two double quotes at the beginning to run?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM