title: Downloading files from SFTP server listed by remote command with Paramiko in Python
body: <p>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 <code>file_path</code> and <code>file_name</code> but not able to download all the files. Below is the code I am using:</p>
  
  <pre><code>import paramiko
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  </code></pre>
  
  <p>ssh.connect(hostname=hostname, username=username, password=password, port=port)</p>
  
  <pre><code>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))
  </code></pre>
  
  <p>I can see the <code>file_path</code> and <code>file_name</code> like below from <code>print</code> 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.</p>
  
  <pre><code>file_path = '/home/blahblah/abc.txt'
  file_name = 'abc.txt'
  file_path = '/home/blahblah/def.txt'
  file_name = 'def.txt'
  </code></pre>
  
  <p>I see one file is downloaded and then i get the following error:</p>
  
  <blockquote>
    <p>FileNotFoundErrorTraceback (most recent call last)</p>
  </blockquote>
  
  <p>Error trace:</p>
  
  <pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):  
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  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'
  </code></pre>