首页 > 解决方案 > 如何使用 python 从 Linux 服务器读取驻留在 Windows 共享文件夹中的数据

问题描述

在我目前的情况下,我的 Linux 服务器上有一个 python 脚本,它可以处理远程 Windows 共享文件夹中的一些 pdf 文件。我目前正在使用 smbclient 与其建立连接,但我无法处理文件,可能我需要首先将所有文件传输到我的 Linux 服务器,因为所有文件的大小都可能非常大,这是不利的。在这种情况下我还能做什么,有没有办法创建一个管道,python 脚本将在其中运行并使用这些文件来给出结果。

代码如下:

from smb.SMBConnection import SMBConnection
userID = '---------'
password = '-------'
client_machine_name = '-----'
server_name = ' '
server_ip = '...'
domain_name = '-------'
conn=SMBConnection(userID,password, client_machine_name, server_name, 
domain=domain_name, use_ntlm_v2=True,is_direct_tcp=True)
conn.connect(server_ip, 445)
shares = conn.listShares()
# storing file names in a list
dirlist = []
for share in shares:
    if not share.isSpecial and share.name not in ['NETLOGON', 'SYSVOL']:
        sharedfiles = conn.listPath(share.name, '/DMDA_2.0_Files/ProcessedBatchDocPdfs/12345678')
        for sharedfile in sharedfiles:
           print(sharedfile.filename)
           dirlist.append(sharedfile.filename)
# retrieve or download file
path = 'DMDA_2.0_Files/ProcessedBatchDocPdfs/12345678/111111.pdf'
with open('abc.pdf','wb') as f:
    conn.retrieveFile(share.name, path, f)

标签: pythonftpsmblibsmbclient

解决方案


为了通过在 Linux 环境中运行的代码更改驻留在 Windows 环境中的文件的内容,必须首先将文件移动到 Linux 环境,即数据应与代码一起驻留在同一台机器或分布式系统上。为了对文件执行编辑任务,必须将第一个文件移动到本地机器上,然后对文件进行编辑,最后将文件移回 Windows 机器,原始文件将被覆盖,因此需要任务将完成。


推荐阅读