2
import paramiko
import os

paramiko.util.log_to_file('logfile.log')

host = "100.10.89.23"
port = 22
transport = paramiko.Transport((host, port))
password = "pass"
username = "user"
transport.connect(username = username, password = password)

sftp = paramiko.SFTPClient.from_transport(transport)

filepath = '/import/TMP'
localpath = 'F:\\Projects\\Test'
sftp.get(filepath, localpath)

sftp.close()
transport.close()

ftp_priv_key is not required to connect to sftp. Suppose I have 10 files in the given sftp path, out of which 6 files are in csv format and other or in different format. My requirement is to copy only the csv format files.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Aquil Abbas
  • 167
  • 2
  • 11

1 Answers1

2

You will need to do something like:

sftp = paramiko.SFTPClient.from_transport(transport)
for filename in sftp.listdir(filepath):
    if filename.endswith('.csv'):
        sftp.get(filename, localpath)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • Making changes the way you suggest & while running the code i got the following error: 1. Python36\lib\site-packages\paramiko\sftp_client.py" line 721 size = self.getfo(remotepath, fl, callback) 2.Python36\lib\site-packages\paramiko\sftp_client.py" line 695 file_size = self.stat(remotepath).st_size 3.Python36\lib\site-packages\paramiko\sftp_client.py" line 413 t, msg = self._request(CMD_STAT, path) 4.Python36\lib\site-packages\paramiko\sftp_client.py" line 730 return self._read_response(num) 5. Python36\lib\site-packages\paramiko\sftp_client.py" line 781 self._convert_status(msg) – Aquil Abbas Feb 27 '17 at 04:18
  • 6. Python36\lib\site-packages\paramiko\sftp_client.py" line 807 raise IOError(errno.ENOENT, text) – Aquil Abbas Feb 27 '17 at 04:19
  • Stephen Rauch,, Its really appreciating if you suggest what's missing? – Aquil Abbas Feb 27 '17 at 04:19
  • This does not help. Which line failed? – Stephen Rauch Feb 27 '17 at 04:26