title: Python ftp download and archive
body: <p>I have been trying to script a code with python to grade the main directory of that ftp and archive it into a the local pc. I am not an amateur coder and python is fairly new to me.
  What I am getting as an error right now is.</p>
  
  <pre><code>File "C:\Users\Ali\Desktop\ftp_archiving_script.py", line 24, in &lt;module&gt;
  ftpDownload = ftp.retrbinary('RETR', filename)
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>    from ftplib import FTP
  import zipfile
  import os
  try:
      import zlib
      compression = zipfile.ZIP_DEFLATED
  except:
      compression = zipfile.ZIP_STORED
  modes = { zipfile.ZIP_DEFLATED: "deflated",
            zipfile.ZIP_STORED: "stored",
      }
  
  #print "Logging in..."
  with FTP('xxx.xxx.xxx') as ftp: #hostname goes here
      ftp.login('xxxx','xxxx') #user followed by pass
      #print "changing to root directory"
      ftp.mlsd('//')
      #print "Accessing files"
      filenames = []
      #print filenames
      ftp.retrlines('NLST', filenames.append)
      try:
          for filename in filenames:
              ftpDownload = ftp.retrbinary('RETR', filename)
              with ZipFile(os.path.join('C:\\','DC_archive.zip'), 'w') as myzip:
                  myzip.write(ftpDownload, compress_type=compression)
                  myzip.close()
      finally:
          #print "closing"
          ftp.close()
      ftp.quit()
  </code></pre>
  
  <p>Can anyone enlighten me on this problem.</p>
  
  <p>Thank you,</p>
  
  <p>Update</p>
  
  <pre><code>try:
      for filename in filenames:
          with io.StringIO() as fObject:
              ftp.retrbinary('RETR %s' %filename, fObject.write)
              with ZipFile(os.path.join('C:\\','DC_archive.zip'), 'w') as myzip:
                  myzip.write(fObject, compress_type=compression)
                  myzip.close()
  </code></pre>
  
  <p>updated Traceback for @fals... Also this is using your code below and not the one I have at the top.</p>
  
  <pre><code>    Traceback (most recent call last):
    File "C:\Users\Ali\Desktop\ftp_archive2.py", line 20, in &lt;module&gt;
      ftpDownload = ftp.retrbinary('RETR ' + filename, f.write)
    File "C:\Python33\lib\ftplib.py", line 424, in retrbinary
      with self.transfercmd(cmd, rest) as conn:
    File "C:\Python33\lib\ftplib.py", line 386, in transfercmd
      return self.ntransfercmd(cmd, rest)[0]
    File "C:\Python33\lib\ftplib.py", line 352, in ntransfercmd
      resp = self.sendcmd(cmd)
    File "C:\Python33\lib\ftplib.py", line 259, in sendcmd
      return self.getresp()
    File "C:\Python33\lib\ftplib.py", line 233, in getresp
      raise error_perm(resp)
  ftplib.error_perm: 550 File not found
  </code></pre>