corrupt download with urllib2
Peter Otten
__peter__ at web.de
Tue Nov 10 08:40:59 EST 2015
Ulli Horlacher wrote:
> if u.getcode() == 200:
> print(u.read(),file=szo,end='')
> szo.close()
> else:
> die('cannot get %s - server reply: %d' % (szurl,u.getcode()))
More random remarks:
- print() gives the impression that you are dealing with text, and using it
with binary strings will produce surprising results when you migrate to
Python 3:
Python 2:
>>> from __future__ import print_function
>>> print(b"foo")
foo
Python 3:
>>> print(b"foo")
b'foo'
- with open(...) ensures that the file is closed when an exception occurs.
It doesn't matter here as your script is going to die() anyway, but using
with is a got habit to get into.
- consider shutil.copyfileobj to limit memory usage when dealing with data
of arbitrary size.
Putting it together:
with open(sz, "wb") as szo:
shutil.copyfileobj(u, szo)
More information about the Python-list
mailing list