Get full computer name from a network drive letter in python
Asked Answered
A

5

8

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?

Allin answered 14/1, 2016 at 22:54 Comment(0)
R
7

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
Robedechambre answered 15/1, 2016 at 10:45 Comment(0)
C
5

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
Contamination answered 14/7, 2017 at 12:5 Comment(0)
I
0

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.

Intersection answered 15/1, 2016 at 6:42 Comment(0)
J
0

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.

Joellenjoelly answered 2/8, 2019 at 16:14 Comment(0)
D
-1

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)

Documentation answered 14/1, 2016 at 23:32 Comment(2)
socket.gethostname() appears to do exactly the same thing as win32api.GetComputerName() in that it returns the name of the local computer I need to be able to return the name of a mapped network drive .uname does not appear to exist as an object in the os module.Allin
i think uname is only available on linux/unix systems. but it would also return the local computer name.Intersection

© 2022 - 2024 — McMap. All rights reserved.