Python paramiko SFTP example

Paramiko Python SSH library provides a convenient SFTPClient that allows easy transfer of files over SSH. A distinction from the command line utility sftp is that “put"ing a file must include the full destination path including filename, to avoid OSError.

This Paramiko example shows copying a file from local computer to remote computer over SSH in Python.

from paramiko import SSHClient

source = "foo.txt"
dest = "~/Documents/foo.txt"

with SSHClient() as ssh:

    ssh.load_system_host_keys()
    ssh.connect("server.invalid")
    with ssh.open_sftp() as sftp:
        sftp.put(source, dest)