-1

When I use this code to download a zip file from and FTP server it comes back corrupted. Anyone know why?

from ftplib import FTP
import getpass

user = raw_input('Username: ')
password = getpass.getpass()
host = raw_input('Host:')
ftp = FTP(host,user,password)
ftp.retrlines('LIST')
f_file = raw_input('What is the name of the file you would like to download? ')
print 'Opening local file...'
l_file = open(f_file, 'w')
print "Getting", f_file
ftp.retrbinary('RETR ' + f_file, l_file.write)
print "Closing", f_file
l_file.close()
print 'Closing FTP connection'
ftp.close()

1 Answers1

3

This is probably because you're writing the local copy in ASCII mode, not binary mode, thereby changing all 0A bytes into 0D0A (LF to CRLF), corrupting the binary file.

Try again using l_file = open(f_file, 'wb').

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561