Get full computer name from a network drive letter in python

那年仲夏 提交于 2019-12-10 04:19:07

问题


I am using python to populate a table with the file pathways of a number of stored files. However the pathway needs to have the full network drive computer name not just the drive letter, ie

//ComputerName/folder/subfolder/file

not

P:/folder/subfolder/file

I have investigated using the win32api, win32file, and os.path modules but nothing is looking like its able to do it. I need something like win32api.GetComputerName() but with the ability to drop in a known drive letter as an argument and it return the computer name that is mapped to the letter.

So is there anyway in python to look up a drive letter and get back the computer name?


回答1:


Network drives are mapped using the Windows Networking API that's exported by mpr.dll (multiple provider router). You can create a network drive via WNetAddConnection2. To get the remote path that's associated with a local device, call WNetGetConnection. You can do this using ctypes as follows:

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

For example:

>>> subprocess.call(r'net use Y: \\live.sysinternals.com\tools')
The command completed successfully.
0
>>> print(get_connection('Y:'))
\\live.sysinternals.com\tools



回答2:


I think you just need to look at more of pywin32... As you can see here, there is already an API that converts local drive names to full UNC paths.

For completeness, here is some code that works for me.

import win32wnet
import sys

print(win32wnet.WNetGetUniversalName(sys.argv[1], 1))

And this gives me something like this when I run it:

C:\test>python get_unc.py i:\some\path
\\machine\test_share\some\path



回答3:


My answer to a similar question:

Here's how to do it in python ≥ 3.4, with no dependencies!*

from pathlib import Path

def unc_drive(file_path):
    return str(Path(file_path).resolve())

*Note: I just found a situation in which this method fails. One of my company's network shares has permissions setup such that this method raises a PermissionError. In this case, win32wnet.WNetGetUniversalName is a suitable fallback.




回答4:


If you just need the hostname, you can use the socket module:

socket.gethostname()

or you may want to use the os module:

os.uname()[1]

os.uname() returns a 5 tuple that contains (sysname, nodename, release, version, machine)




回答5:


you could run net use and parse the output.

i am posting this from my mobile but i am going to improve this answer when i am in front of a real computer.

here are some links, that can help in the meantime: https://docs.python.org/2/library/subprocess.html#module-subprocess.
https://technet.microsoft.com/en-us/library/gg651155.aspx.



来源:https://stackoverflow.com/questions/34801315/get-full-computer-name-from-a-network-drive-letter-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!