Home > Blockchain >  How to take data from SFTP server without downloading files in Python?
How to take data from SFTP server without downloading files in Python?

Time:02-05

I have an SFTB server. I can take data by transferring/downloading files. Is there a way that I can do without downloading files?

My code is as below:

    def connect(self):
        """Connection to the sftp server"""
        with pysftp.Connection(self.hostname,self.username,self.passowrd,self.port) as sftp:

            with sftp.cd('directory'):
                sftp.get('filename.txt')


This code downloads file to my local machine.

CodePudding user response:

Yes and no. You can use the data from the remote (SFTP) server without storing the files to a local disk.

But you cannot use data locally without downloading them. That's impossible. You have to transfer the data to use them – at least to a memory of the local machine.

See A way to load big data on Python from SFTP server, not using my hard disk.

My answer there talks about Paramiko. But pysftp is a just a thin wrapper around Paramiko. Its Connection.open is directly mapped to underlying Paramiko's SFTPClient.open. So you can keep using pysftp:

with sftp.open('filename.txt', bufsize=32768) as f:
    # use f as if you have opened a local file with open()

Though I'd recommend you not to: pysftp vs. Paramiko.

  •  Tags:  
  • Related