Search code examples
pythonpython-3.xsftppysftp

How to take data from SFTP server without downloading files in my local storage in Python?


I have an SFTP 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:

# Connection to the SFTP server
with pysftp.Connection(hostname, username, passowrd, port) as sftp:
    with sftp.cd('directory'):
        sftp.get('filename.txt')

This code downloads file to my local machine.


Solution

  • 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.