title: Is there a special trick to downloading a zip file and writing it to disk with Python?
body: <p>I am FTPing a zip file from a remote FTP site using Python's ftplib. I then attempt to write it to disk. The file write works, however most attempts to open the zip using WinZip or WinRar fail; both apps claim the file is corrupted. Oddly however, when right clicking and attempting to extract the file using WinRar, the file <em>will</em> extract.</p>
  
  <p>So to be clear, the file write will work, but will not <em>open</em> inside the popular zip apps, but <em>will</em> decompress using those same apps. Note that the Python zipfile module <em>never</em> fails to extract the zips.</p>
  
  <p>Here is the code that I'm using to get the zip file from the FTP site (please ignore the bad tabbing, that's not the issue). </p>
  
  <pre><code>filedata = None
  def appender(chunk):
      global filedata
      filedata += chunk
  
  
  def getfile(filename):
    try:
        ftp = None
  
        try:
            ftp = FTP(address)
            ftp.login('user', 'password')
  
        except Exception, e:
            print e
  
        command = 'RETR ' + filename
  
        idx = filename.rfind('/')
        path = filename[0:idx]
        ftp.cwd(path)
        fileonly = filename[idx+1:len(filename)]
  
        ftp.retrbinary('RETR ' + filename, appender)
  
        global filedata
        data = filedata
  
        ftp.close()
  
        filedata = ''
        return data
  
    except Exception, e:
        print e
  
  data = getfile('/archives/myfile.zip')    
  file = open(pathtoNTFileShare, 'wb')
  file.write(data)
  file.close()
  </code></pre>