Implementing a SFTP Client Using Python and Paramiko

By | April 28, 2016

This post shows how to use the Python library Paramiko to implement a SFTP client that can be used to programatically send and receive files over SFTP. As when using Paramiko for SSH communication, authentication is performed using either username and password or username and a private key.


The preparations are identical to those described in my previous post on SSH communication using Paramiko, please refer to that post for details.


I have written two functions that both create a SFTPClient object connected to a computer on which remote file operations can be performed. The two functions differ slightly in that the first one uses a Paramiko Transport object to establish a connection to the (remote) computer and then create the SFTClient object using the Transport object. The second function, on the other hand, creates a Paramiko SSHClient object which is then used to open a SFTP connection and obtain a SFTPClient object.

The code above establishes a SFTP connection to the same computer on which it executes (localhost) on port 22 using the user-name “ivan” and the password “secretpassword” and then retrieves the directory listing of the current directory, downloads a file and uploads a file.
If you want to use a private key for authorization, set the variable keyfile_path to contain the absolute or relative path to a file containing the private key. Don’t forget to change the keyfile type from ‘DSA’ to ‘RSA’ if your private key is a RSA key.

If you now run the program, a directory listing should be printed to the console, a new file named ‘downloaded_file’ should appear and, in the remote directory, a file named ‘remote_testfile.txt’ should appear.

Happy coding!

5 thoughts on “Implementing a SFTP Client Using Python and Paramiko

  1. UC

    Thanks very much for the guide, however I get the following errors – desperate to get this working, all I had done was supplied host, username, keyfile path and password for paramters at the bottom and also changed DSA to RSA:

    Traceback (most recent call last):
    An error occurred creating SFTP client: : ‘str’ object has no attribute ‘readlines’
    File “C:/Users/uc/PycharmProjects/WMDaten/”, line 94, in
    dirlist = sftpclient.listdir(‘.’)
    AttributeError: ‘NoneType’ object has no attribute ‘listdir’

    Please help, Thanks you

  2. TooLate

    Line 23 should be:
    key = paramiko.RSAKey.from_private_key_file(keyfilepath) , that’s why RSA keys don’t work, just a typo.

  3. RSABear

    thank you so much Ivan! I was having endless problems with paramiko and it was all my fault – if only I found your example 2 weeks ago I would have created the transport correctly

  4. Jim Dunn

    Help! Trying to execute the above example but keep getting the same error I cannot clear.
    I picked up on the typo below but still not executing.

    It appears to not be able to create the transport object.
    Thanks, Jim

    host = ‘’
    port = 22
    username = ‘root’
    password = ‘secret’
    keyfile_path = None

    C:\Users\james.dunn\PycharmProjects\RemoteCollector\venv\Scripts\python.exe C:/Users/james.dunn/PycharmProjects/RemoteCollector/
    An error occurred creating SFTP client: : EOF during negotiation
    Traceback (most recent call last):
    File “C:\Users\james.dunn\PycharmProjects\RemoteCollector\”, line 98, in
    dirlist = sftpclient.listdir(“/mnt/mmcblk0p1/”)
    AttributeError: ‘NoneType’ object has no attribute ‘listdir’

    Process finished with exit code 1


Leave a Reply

Your email address will not be published. Required fields are marked *