downloading files

kyosohma at gmail.com kyosohma at gmail.com
Fri Aug 3 15:15:35 EDT 2007


On Aug 3, 1:48 pm, Ehsan <ehsan.khod... at gmail.com> 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.read())
>         webFile.close()
>         localFile.close()
> download('http://www.2shared.com/download/1839752/cd520048/
> xpersia14.3gp?tsid=20070803-143313-49566ea2', 'xpersia4.3gp' )

Uhhh...you 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.

Mike




More information about the Python-list mailing list