假设Windows计算机上有一个通过网络共享的本地驱动器。
该驱动器在本地映射到D:\,并由本地计算机通过名为data的网络共享。因此,此驱动器的网络路径为\\computer-name\data。
在给定主机驱动器号D的情况下,是否可以在Python语言中以编程方式确定共享网络路径的名称?
预期的行为将是:
drive_letter = "D"
get_network_path(drive_letter)
>>> \\computer-name\data唯一的附加限制是,这应该在没有管理员权限的情况下工作。
发布于 2019-05-10 21:53:57
通过使用带有net share的subprocess模块,我能够解析完整的网络路径,该模块将列出所有共享驱动器。
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\datahttps://stackoverflow.com/questions/56048636
复制相似问题