Python挂载杂记

python 挂盘的一些杂记

import os

'''
Mount(挂载) 网络磁盘
Params(参数需求):
    local               本地挂载盘符      例如: local = "Z:"
    remote              远程地址, 需要挂载的文件夹         例如: remote = "\\192.168.1.111	est"     
    **args  remote_user         远程用户          
    **args  remote_password     远程用户密码
'''
def mount_add(local, remote, **args):
    info = {}
    for key,value in args.items():
        info[key] = value
    if info == {}:
        print("Mount Add Start......")
        # 没有传远程用户和密码, 直接连接
        result = os.system("net use "+ local + " " + remote)
    else:# 传了使用用户密码连接
        print("Mount Add Start......")
        result = os.system("net use "+ local + " " + remote  + " /USER:" + info["remote_user"] + " " + info["remote_password"])
    # 挂载网络共享
    if result == 0:
        print(result)
        print("挂载成功")
        print("Remote directory successfully mounted")


def mount_delete(local):
    # 卸载网络共享
    print("开始卸载......")
    # result2 = os.system("net use " + local + " /delete" + " /y")     # 卸载 local 地址
    result2 = os.system("net use " + "*" + " /delete" + " /y")          # 移除所有挂载
    # /y代表确认删除, 不然命令行会出确认提示,阻塞程序
    # net use */d /y        命令行命令
    if result2 == 0:
        print(result2)
        print(f"成功卸载{local}")

def mount_path(MountMap):       
    print("net ust start")
    if isinstance(MountMap, dict):      # 判断mountMap是不是字典
        os.system("net use * /del /y")  # 卸载所有挂载
        print("net use * /del /y")
        print('mntMap: ', MountMap)     # 打印卸载的地址
    for key in MountMap:                # 遍历map进行挂载
        # newPath = configJson["mntMap"][key].replace("/", "\").replace(replacePath, "\")
        newPath = MountMap[key]
        print("newPath:", newPath)

        os.system('net use "' + key + '" "' + newPath + '"', )      # 挂载
    print("net ust++++end")
    print("AnalysisMaya.webNetPath.end")



if __name__ == "__main__":
    # local = "H:"
    # remote = "\\192.168.1.111	estmax"
    # mount_user_name = "Administrator"
    # mount_password = "3210456"

    # mount_add(local=local, remote=remote)       # 不带用户名密码挂载
    # mount_add(local=local, remote=remote, remote_user=mount_user_name, remote_password=mount_password)

    # mount_delete("hh")

    MountMap = {
                "D:" : "\\192.168.1.111\test",
    }
    mount_path(MountMap)
原文地址:https://www.cnblogs.com/MasonHu/p/13931591.html