Reputation: 65
I am using Paramiko to connect to the SFTP server from my local machine and download txt files from remote path. I am able to make successful connection and can also print the remote path and the files but i cannot get the files locally. I can print the file_path
and file_name
but not able to download all the files. Below is the code I am using:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=hostname, username=username, password=password, port=port)
remotepath = '/home/blahblah'
pattern = '"*.txt"'
stdin,stdout,stderr = ssh.exec_command("find {remotepath} -name {pattern}".format(remotepath=remotepath, pattern=pattern))
ftp = ssh.open_sftp()
for file_path in stdout.readlines():
file_name = file_path.split('/')[-1]
print(file_path)
print(file_name)
ftp.get(file_path, "/home/mylocalpath/{file_name}".format(file_name=file_name))
I can see the file_path
and file_name
like below from print
statement but get error while using ftp.get for multiple files. I can copy a single file by hardcoding the name on source and destination.
file_path = '/home/blahblah/abc.txt'
file_name = 'abc.txt'
file_path = '/home/blahblah/def.txt'
file_name = 'def.txt'
I see one file is downloaded and then i get the following error:
FileNotFoundErrorTraceback (most recent call last)
Error trace:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "...anaconda3/lib/python3.6/site-packages/paramiko/sftp_client.py", line 769, in get
with open(localpath, 'wb') as fl:
FileNotFoundError: [Errno 2] No such file or directory: 'localpath/abc.txt\n'
Upvotes: 0
Views: 4619
Reputation: 202168
readlines
does not remove newline from the line. So as you can see in the traceback, you are trying to create a file named abc.txt\n
, what is not possible on many file systems, and mainly, it's not what you want.
Trim the trailing new lines from file_path
:
for file_path in stdout.readlines():
file_path = file_path.rstrip()
file_name = file_path.split('/')[-1]
# ...
Though you would have saved yourself lot of troubles, had you used a pure SFTP solution, instead of hacking it by executing a remote find
command (what is a very fragile solution, as hinted in comments by @CharlesDuffy).
See List files on SFTP server matching wildcard in Python using Paramiko.
Side note: Do not use AutoAddPolicy
. You
lose security by doing so. See Paramiko "Unknown Server".
Upvotes: 2