0

Consider a local drive on a Windows machine that is shared over a network.

The drive is mapped to D:\ locally and it is shared by the local machine over the network with the name data. Therefore, the network path for this drive would be \\computer-name\data.

Is it possible to determine the name of the shared network path programatically in Python given the drive letter D from the host machine?

Expected behavior would be:

drive_letter = "D"
get_network_path(drive_letter)
>>> \\computer-name\data

The only additional constraint is that this should work without admin permissions.

Luke DeLuccia
  • 541
  • 6
  • 16
  • Does this answer your question? [Get full computer name from a network drive letter in python](https://stackoverflow.com/questions/34801315/get-full-computer-name-from-a-network-drive-letter-in-python) – matt2000 Jul 10 '21 at 00:29

1 Answers1

0

I was able to resolve the full network path by using the subprocess module with net share, which will list all shared drives.

import platform
import subprocess


def get_network_path(drive_letter: str):
    s = subprocess.check_output(['net', 'share']).decode()  # get shared drives
    for row in s.split("\n")[4:]:  # check each row after formatting
        split = row.split()
        if len(split) == 2:  # only check non-default shared drives
            if split[1] == '{}:\\'.format(drive_letter):
                return r"\\{}\{}".format(platform.node(), split[0])

print(get_network_path("D"))   
>>> \\computer-name\data
Luke DeLuccia
  • 541
  • 6
  • 16