title: How to copy/download remote files to local (sftp)?
body: <p>Trying to copy the remote files/directories to local machine, but getting error</p>
  
  <pre><code>Traceback (most recent call last):
    File "test_ssh.py", line 23, in &lt;module&gt;
      sftp_client.put(str(os.path.join(remote_dir,file)),os.path.join(local_dir, file))
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/sftp_client.py", line 757, in put
      file_size = os.stat(localpath).st_size
  OSError: [Errno 2] No such file or directory: 
  </code></pre>
  
  <p>Not sure, what is wrong here. Below is the snippet</p>
  
  <pre><code>import paramiko
  import os
  import stat
  
  
  remote_host = "10.10.10.10"
  username = "user"
  key_filename = "/path/to/key_filename"
  port = 22
  
  remote_path = "/remote/path/location"
  
  latest_folder_name = "abc_123456"
  local_dir = os.path.join(os.getcwd(),latest_folder_name)
  remote_dir = os.path.join(remote_path, latest_folder_name)
  
  
  ssh = paramiko.SSHClient()
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  ssh.connect(remote_host, username=username, key_filename=key_filename, port=port)
  sftp = ssh.open_sftp()
  
  for file in sftp.listdir(remote_dir):
      if stat.S_ISREG(sftp.stat(os.path.join(remote_dir, file)).st_mode):
          print "FILE '{}'".format(file)
          sftp.put(os.path.join(remote_dir,file),os.path.join(local_dir, file))
      else:
          print "DIRECTORY {}".format(file)
  sftp.close()
  </code></pre>
  
  <p>What could be wrong here ? </p>
  
  <p>when tried to download with <code>sftp.get(remotefile,localfile)</code>, error</p>
  
  <pre><code>Traceback (most recent call last):
    File "test_ssh.py", line 23, in &lt;module&gt;
      sftp.get(os.path.join(remote_dir,file),os.path.join(local_dir, file))
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/paramiko/sftp_client.py", line 801, in get
      with open(localpath, "wb") as fl:
  IOError: [Errno 2] No such file or directory: 
  </code></pre>