我正在使用 python 来填充一个表,其中包含多个存储文件的文件路径。但是,该路径需要具有完整的网络驱动器计算机名称,而不仅仅是驱动器号,即
//计算机名/文件夹/子文件夹/文件
not
P:/文件夹/子文件夹/文件
我已经研究过使用 win32api、win32file 和 os.path 模块,但看起来没有任何东西能够做到这一点。我需要类似的东西win32api.GetComputerName()
但能够将已知的驱动器盘符作为参数放入,并返回映射到该盘符的计算机名称。
那么Python中有没有办法查找驱动器号并返回计算机名称呢?
网络驱动器使用映射视窗网络由 mpr.dll(多提供商路由器)导出的 API。您可以通过以下方式创建网络驱动器WNetAddConnection2。要获取与本地设备关联的远程路径,请调用WNetGetConnection。您可以使用 ctypes 执行此操作,如下所示:
import ctypes
from ctypes import wintypes
mpr = ctypes.WinDLL('mpr')
ERROR_SUCCESS = 0x0000
ERROR_MORE_DATA = 0x00EA
wintypes.LPDWORD = ctypes.POINTER(wintypes.DWORD)
mpr.WNetGetConnectionW.restype = wintypes.DWORD
mpr.WNetGetConnectionW.argtypes = (wintypes.LPCWSTR,
wintypes.LPWSTR,
wintypes.LPDWORD)
def get_connection(local_name):
length = (wintypes.DWORD * 1)()
result = mpr.WNetGetConnectionW(local_name, None, length)
if result != ERROR_MORE_DATA:
raise ctypes.WinError(result)
remote_name = (wintypes.WCHAR * length[0])()
result = mpr.WNetGetConnectionW(local_name, remote_name, length)
if result != ERROR_SUCCESS:
raise ctypes.WinError(result)
return remote_name.value
例如:
>>> subprocess.call(r'net use Y: \\live.sysinternals.com\tools')
The command completed successfully.
0
>>> print(get_connection('Y:'))
\\live.sysinternals.com\tools
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)