1
votes

I´m struggling with python and hit the wall here after a lot of reading.

I´ve set up a server with a zip file named bk_data.zip and want to download and extract the content. The zip file is downloaded, but the data is corrupt and can´t be extracted. I´ve read about the zipfile objects (docs.python.org/2.7/library/zipfile.html) and tried different mode parameters, but without succes. Help is appreciated. This is my code:

import urllib2
import zipfile

my_download_url = "http://xx.xxx.xx.x/bk_data.zip"
request = urllib2.urlopen(my_download_url)

output = open("C:/that/very/long/path/bk_data.zip", "w")
output.write(request.read())
output.close()

with zipfile.ZipFile("C:/that/very/long/path/bk_data.zip', "r") as z:
    z.extractall("C:/that/very/long/path/")
1
try changing "w" and "r" to "wb" and "rb" to open the files in binary mode - maxymoo
That worked maxymoo. Thank you. Please add is as your answer. - CARTOS
no worries, glad it worked - maxymoo
I really struggled with this, and yet so simple. Python is still challenging, but fun for me :-) - CARTOS
yeah there's a few little quirks like this one that take quite a while to get used to ... another one to watch out for is latin-1 vs utf-8 encoding for strings if you're working on Windows. - maxymoo

1 Answers

1
votes

You need to change "w" and "r" to "wb" and "rb" to open the files in binary mode.