Python 2: Get network share path from drive letter

Robben_Ford_Fan_boy picture Robben_Ford_Fan_boy · Jul 7, 2017 · Viewed 7k times · Source

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

Answer

Peter Brittain picture Peter Brittain · Jul 14, 2017

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