如何测试给定路径是否为装入点(Windows)

2024-06-16 11:02:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找python代码来测试给定的路径是否不在windows10上的本地驱动器上(即它已挂载)。如果路径是K:\dir1\file2.txt,我想知道它是本地文件还是在网络上。 我检查了互联网,发现了一个只适用于Posix的解决方案,比如os.path.ismount(path),但它在Win10上不起作用。它只适用于\\server\path

我玩w/ctypes.windll.kernel32文件.GetFileAttributesA()(灵感来源于:https://docs.microsoft.com/en-us/windows/desktop/fileio/determining-whether-a-directory-is-a-volume-mount-point),但没有很好的结果。你知道吗

请帮忙


Tags: 文件path代码路径网络txtos互联网
1条回答
网友
1楼 · 发布于 2024-06-16 11:02:34

您可以使用psutil

从路径获取sdiskpart信息的工作方法

def find_mount_point(path):
    path = os.path.abspath(path)
    while not os.path.ismount(path):
        path = os.path.dirname(path)
    p = [p for p in psutil.disk_partitions(all=True) if p.mountpoint == path.__str__()]
    l = len(p)
    if len(p) == 1:
        print type(p[0])
        return p[0]
    raise psutil.Error

如果你的驱动器是远程的

p = find_mount_point("X:")
print p.opts

你应该回来

rw,remote

(见https://psutil.readthedocs.io/en/latest/

要从驱动器lettre获取UNC(知道它是远程的),可以使用win32wnet

import win32wnet

获取diskpart(p)的代码

print win32wnet.WNetGetUniversalName(p.mountpoint, 1)

将打印

\\My_Drive\my_folder

相关问题 更多 >