如果我使用以下方法来获取所有连接驱动器的列表:的Python 2:从驱动器盘符获取网络共享路径
available_drives = ['%s:' % d for d in string.ascii_uppercase if os.path.exists('%s:' % d)]
我怎么连接的驱动器的UNC路径?
os.path
刚刚返回的z:\
代替
如果我使用以下方法来获取所有连接驱动器的列表:的Python 2:从驱动器盘符获取网络共享路径
available_drives = ['%s:' % d for d in string.ascii_uppercase if os.path.exists('%s:' % d)]
我怎么连接的驱动器的UNC路径?
os.path
刚刚返回的z:\
代替
使用win32wnet从pywin32到您的驱动器字母转换。例如:
import win32wnet
import sys
print(win32wnet.WNetGetUniversalName(sys.argv[1], 1))
这给了我这样的事情,当我运行它:
C:\test>python get_unc.py i:\some\path
\\machine\test_share\some\path
使用,并在这个职位的第一个答案显示的代码:Get full computer name from a network drive letter in python,就可以得到每一个网络驱动器,或选择几个驱动器路径。
给出如果驱动器不是网络驱动器,无论是本地或可移动驱动器将抛出一个错误get_connection
功能,这可占具有
# your drive list
available_drives = ['%s:' % d for d in string.ascii_uppercase if os.path.exists('%s:' % d)]
for drive in available_drives:
try:
# function from linked post
print(get_connection(drive))
except WindowsError: # thrown from local drives
print('{} is a local drive'.format(drive))
注:我没有尝试执行此。 https://docs.python.org/2/library/os.path.html基于此,“注意在Windows上,许多这些函数不能正确支持UNC路径名。splitunc()和ismount()确实正确地处理它们”。 os.path.splitunc(path)将路径名路径拆分为一对(unc,rest),以便unc是UNC挂载点(如r'\\ host \ mount')(如果存在),并将其余部分路径(如r'\ path \ file.ext')。 对于包含驱动器号的路径,unc将始终为空字符串。 – prashanth
在博客文章中尝试以下库。 http://developer.covenanteyes.com/unc-paths-with-python/自从我这样做以来,http://covenanteyes.github.io/py_win_unc/ – prashanth
的链接已经有一段时间了,但是我发现了iirc就是调用'net show'(sp?)并解析输出......没有任何附近的Windows机器现在就试用它... –