downloading files

Steve Holden steve at holdenweb.com
Fri Aug 3 15:10:01 EDT 2007


Ehsan 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' )
> 
I'm guessing there are binary files and you are running on Windows, 
which is inserting a carriage return before ebery newline. Try

	localFile = open(fileName, 'wb')

to avoid thus behavior.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list