downloading files

kyosohma at kyosohma at
Fri Aug 3 21:15:35 CEST 2007

On Aug 3, 1:48 pm, Ehsan <ehsan.khod... at> wrote:
> I foundd this code in ASPN  Python Cookbook for downloading files in
> python but when it finished downloading files the files became
> corrupted and didn't open, the files in internet havn't any problem:
> def download(url,fileName):
>         """Copy the contents of a file from a given URL
>         to a local file.
>         """
>         import urllib
>         webFile = urllib.urlopen(url)
>         localFile = open(fileName, 'w')
>         localFile.write(
>         webFile.close()
>         localFile.close()
> download('
> xpersia14.3gp?tsid=20070803-143313-49566ea2', 'xpersia4.3gp' ) probably need to change the open() command to binary mode.
Replace that line with this:

localFile = open(fileName, mode='wb')

I tried it on my PC to download a photo from one of my sites and it
worked great.


More information about the Python-list mailing list