Downloading file with pysftp

后端 未结 1 1408
星月不相逢
星月不相逢 2021-02-05 11:13

I\'m trying to load (and directly save locally) a .csv file stored on a FTP Server (SFTP protocol). I\'m using Python in combination with pysftp library. When I che

相关标签:
1条回答
  • 2021-02-05 11:51

    Connection.get does not return anything. It downloads the remote file to a local path specified by the localpath argument. If you do not specify the argument, it downloads the file to the current working directory.

    So if you want to download to a specific local directory instead, you want this:

    sftp.get('directory/file.csv', '/local/path/file.csv')
    

    If you really want to read the file to a variable (what I understand that you actually do not want), you need to use Connection.getfo, like:

    flo = BytesIO()
    sftp.getfo(remotepath, flo)
    flo.seek(0)
    

    Alternatively, use Paramiko library directly (without the pysftp wrapper).
    See Read a file from server with SSH using Python.


    Obligatory warning: Do not set cnopts.hostkeys = None, unless you do not care about security. For the correct solution see Verify host key with pysftp.

    0 讨论(0)
提交回复
热议问题