Python - How to open Windows share using user name and password

后端 未结 4 1582
礼貌的吻别
礼貌的吻别 2020-12-10 14:57

I would like to access Windows share (ex. \\backupserver\\backups) from Python script. Share is protected by user name and password. How to open this share using user name a

4条回答
  •  青春惊慌失措
    2020-12-10 15:32

    Complete example for "NET USE":

    backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)
    
    if backup_storage_available:
        logger.info("Backup storage already connected.")
    else:
        logger.info("Connecting to backup storage.")
    
        mount_command = "net use /user:" + BACKUP_REPOSITORY_USER_NAME + " " + BACKUP_REPOSITORY_PATH + " " + BACKUP_REPOSITORY_USER_PASSWORD
        os.system(mount_command)
        backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)
    
        if backup_storage_available:
            logger.fine("Connection success.")
        else:
            raise Exception("Failed to find storage directory.")
    

提交回复
热议问题