【问题标题】:Python access and list the files in network share with username and passwordPython使用用户名和密码访问并列出网络共享中的文件
【发布时间】:2016-08-26 09:24:17
【问题描述】:

我想使用 Python 语言从给定的 FileShare 位置获取文件名列表(仅限)。这是我的代码 sn-p,但它没有运行列出任何文件。

    import os
    from os import walk
    SQLSR_USER= 'username'
    SQLSR_PASS= 'password'
    BACKUP_REPOSITORY_PATH= '\\fileshare\location'
    fileList = []
    backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)
    if backup_storage_available:
            print("Backup storage already connected.")
    else:
        print("Connecting to backup storage.")

        mount_command = "net use /user:" + SQLSR_USER + " " + BACKUP_REPOSITORY_PATH + " " + SQLSR_PASS
        os.system(mount_command)
        backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)    
        if backup_storage_available:
            print ("Connection success.")
        else:
            raise Exception("Failed to find storage directory.")

    for (dirpath, dirnames, filenames) in walk(backup_storage_available):
         fileList.extend(filenames)
         break

    if (len(fileList) > 1):
        print "\n\n *********************Required data files are present in the FileShare*********************"

    else:
        print "\n\n ********************* No files are present to start the Next Run *********************"

我仍然对列出的 NET USE 连接命令有问题

    mount_command = "net use /user:" + SQLSR_USER + " " + BACKUP_REPOSITORY_PATH + " " + SQLSR_PASS
    os.system(mount_command)
    backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)

【问题讨论】:

  • try r'\\fileshare\location',否则,错误信息是什么?
  • 是的,它有效。感谢你的及时回复。但是,如何断开它,这意味着在我的代码中我已经提示“备份存储已连接”,所以我无法检查“NET USE”选项请建议使用断开脚本!
  • 小提示:要格外小心,SQLSR_USER/PATH 和 BACKUP_REPO_PATH 不能像 web 服务或其他东西一样远程填写(或彻底检查它们)。那个 os.system 调用可能会给你带来一些麻烦
  • "backup_storage_available = os.path.isdir(BACKUP_REPOSITORY_PATH)" 该代码块返回 false,因为该远程路径未经过身份验证。

标签: python python-2.7 subprocess


【解决方案1】:

最后我使用 pywin32 模块解决了我的问题。下面是它的代码。

    def wnet_connect(host, username, password):
        unc = ''.join(['\\\\', host])
        try:
            win32wnet.WNetAddConnection2(0, None, unc, None, username, password)
        except Exception, err:
            if isinstance(err, win32wnet.error):
                # Disconnect previous connections if detected, and reconnect.
                if err[0] == 1219:
                    win32wnet.WNetCancelConnection2(unc, 0, 0)
                    return wnet_connect(host, username, password)
            raise err

    # Ensure required data files are present in Isilon landing zone of that application context(Eg: gwhp)
    def isLZFilesExist(SHAREPATH):
        wnet_connect(HOST, USER, PASS)        
        path = ''.join(['\\\\', HOST, '\\', SHAREPATH.replace(':', '$')])
        fileList = []
        if os.path.exists(path):
            for (dirpath, dirnames, filenames) in walk(path):
                fileList.extend(filenames)
                break
            if (len(fileList) > 1):
                print fileList
                print "\n\n *********************Required data files are present in the FileShare of that application context(Eg: gwhp)*********************"
                excelResult[0]='PASS'
            else:
                print "\n\n ********************* No files are present to start the Next Run *********************"
                sys.exit(0)

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多