Search code examples
pythonpython-2.7uncnetwork-share

Python 2: Get network share path from drive letter


If I use the following to get the list of all connected drives:

available_drives = ['%s:' % d for d in string.ascii_uppercase if os.path.exists('%s:' % d)]

How do I get the UNC path of the connected drives?

os.path just returns z:\ instead of \share\that\was\mapped\to\z


Solution

  • Use win32wnet from pywin32 to convert your drive letters. For example:

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

    This gives me something like this when I run it:

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