Python ftplib does not download completely
Question
I am using python ftplib to download builds from the builds ftp server. The files are about 1.5-1.6 GB. I use a batch file to run the program. This way it is easier to schedule the download. The problem is, downloaded files do not seem to work. They throw a compatibility error(windows). If I download the files using FileZilla, the files work fine. Also, there is a difference of a few hundred B on the source file and the downloaded file. What is going on?
import ftplib, sys, os
ftp = ftplib.FTP("<server_name")
try:
ftp.login(user= "<user>", passwd = "<password>")
except:
sys.stderr.write('Could not login.')
data=[]
ftp.dir(data.append)
builds=[]
trg=0
trg_bld=""
for i in data:
if len(i.split(" "))>12:
if len(i.split(" ")[12].split("_"))>2:
#this line is to find the version on the server
if (i.split(" ")[12].split("_")[1]== "3.1.0"):
if int(i.split(" ")[12].split("_")[2])>trg:
trg_bld = i.split(" ")[12]
trg_file = trg_bld
print trg_file
if os.path.isfile(trg_file):
sys.stderr.write('File already exists.')
sys.exit(1)
f= open(trg_file, "w")
ftp.retrbinary('RETR '+trg_bld, f.write)
f.close()
ftp.close()
sys.stdout.write("File download successful.")
Batch file: E: cd E:\Builds python ftp_sch.py pause
Solution
f= open(trg_file, "w")
-> f= open(trg_file, "wb")
Without the b
flag Python thinks you're writing ASCII and it's changing the line-endings (hence the size difference and broken binaries).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow