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
Via File Explorer To check the path of a network drive using File Explorer, click on 'This PC' on the left panel in Explorer. Then double-click the mapped drive under 'Network Locations'. The path of the mapped network drive can be seen at the top.
In Windows, if you have mapped network drives and you don't know the UNC path for them, you can start a command prompt (Start → Run → cmd.exe) and use the net use command to list your mapped drives and their UNC paths: C:\>net use New connections will be remembered.
Here's how. Find the file or folder whose path you'd like to copy in File Explorer. Hold down Shift on your keyboard and right-click on it. In the context menu that pops up, select “Copy As Path.”
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With