By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

downloading files

P: n/a
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' )

Aug 3 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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 -------------

Aug 3 '07 #2

P: n/a
On Fri, 2007-08-03 at 11:48 -0700, 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')
[...]
Try 'wb' instead of 'w'.

--
Carsten Haese
http://informixdb.sourceforge.net
Aug 3 '07 #3

P: n/a
On Aug 3, 1:48 pm, Ehsan <ehsan.khod...@gmail.comwrote:
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

Aug 3 '07 #4

P: n/a
On Aug 3, 10:10 pm, Steve Holden <st...@holdenweb.comwrote:
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 -------------- Hide quoted text -

- Show quoted text -
thanx Steve
It works but could you explain more what's wrong with just 'w'?

Aug 3 '07 #5

P: n/a
Il Fri, 03 Aug 2007 14:32:19 -0700, Ehsan ha scritto:

It works but could you explain more what's wrong with just 'w'?
On Unix-like systems newline means '\n'

On Window newline means '\r\n'

So, when you open a file on Window with 'w' option, Win replace
downloaded '\n' with a local '\r\n' and the file isn't readable. If you
use 'wb' you say that you want a binary copy ('b' option) and Win doesn't
replace anything.

Hope it is useful,

bye
Fabio
Aug 3 '07 #6

P: n/a
Ehsan wrote:
On Aug 3, 10:10 pm, Steve Holden <st...@holdenweb.comwrote:
[...]
>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.
[...]
thanx Steve
It works but could you explain more what's wrong with just 'w'?
Hmm, I thought I had. 'b' stand for 'binary', and the system sends out
exactly the bytes you right. Without the 'b' it assumes you are handling
text, so Windows "CR/LF" line endings are converted to "LF" or reading,
and "LF" is converted to "CR/LF" on writing.

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 -------------
Aug 4 '07 #7

P: n/a
Ehsan wrote:
On Aug 3, 10:10 pm, Steve Holden <st...@holdenweb.comwrote:
[...]
>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.
[...]
thanx Steve
It works but could you explain more what's wrong with just 'w'?
Hmm, I thought I had. 'b' stand for 'binary', and the system sends out
exactly the bytes you right. Without the 'b' it assumes you are handling
text, so Windows "CR/LF" line endings are converted to "LF" or reading,
and "LF" is converted to "CR/LF" on writing.

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 -------------

Aug 4 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.