1

I want to write a Python script that connects to the remote SFTP server and creates a ZIP file in the remote server which consists of specific files present in remote server itself. I have written the below script, I m using pysftp for this,

with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword, cnopts=cnopts) as sftp:
    print("Connection succesfully stablished ... ")
    files = ['user/1.csv','user/2.csv','user/test.csv', 'user/test1.csv']
    with zipfile.ZipFile('zipfile.zip', 'w') as zipMe:
        for file in files:
            zipMe.write(file, file.split('/')[-1], compress_type=zipfile.ZIP_DEFLATED)

But instead of creating a ZIP file in the remote server, an ZIP file gets created in the local. Can anyone please help me with this?

0

1 Answer 1

1

Indeed, you code compresses local files to local ZIP archive. Note how your code never uses the sftp variable.

If you want to compress remote files to remote ZIP archive using a local Python script, you have to download the remote files, zip them locally and upload the ZIP archive.

You can do that all that "in-memory" without actually storing the remote files or the ZIP file physically to the local system. But you still need to do all the transfers. So it will be terribly inefficient.


You better execute zip command on the remote server using your SFTP (or SSH actually) connection.

Related question: How to decode Zip file from sftp file using paramiko python

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.