pythonftpsmblibsmbclient

How to read data residing on Windows Shared folder from a Linux Server using python


In my current situation I have a python script on my Linux server which would work on some pdf files residing on a remote windows shared folder. I am currently using smbclient to make connection to it but I am not able to work on the files, probably I need to first transfer all files to my Linux server for that which is not favorable as size of all files can be very huge. What else can I do in this situation, is there a way to create a pipeline in which python script would run and use those files to give result.

Code is as follows:

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)

Solution

  • For changing the contents of a file residing on an Windows environment by the code running on Linux environment the file has to be moved to the Linux environment first that is the data should reside along with the code on the same machine or a distributed system. For performing the edit task on a file, first file has to be moved on the local machine then the edits to the file are made and then the file is finally moved back to the windows machine where the original file will be overwritten, thus the desired task will be completed.