python sftp list files directory code example

Example 1: paramiko count file

import paramiko

ip   =  'your remote ip'
username  =  'your user_name'
password  =  'your password'

path = 'your path you want to count file'

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=username, password=password)
sftp = ssh.open_sftp()
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('cd ' + path + ' && ls | wc -l')

print(ssh_stdout.read().decode("utf-8"))

Example 2: python sftp put file

import pysftp

srv = pysftp.Connection(host="www.destination.com", username="root",
password="password",log="./temp/pysftp.log")

with srv.cd('public'): #chdir to public
    srv.put('C:\Users\XXX\Dropbox\test.txt') #upload file to nodejs/

# Closes the connection
srv.close()