472,114 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,114 software developers and data experts.

downloading files

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

Similar topics

4 posts views Thread by Luke StClair | last post: by
reply views Thread by sales | last post: by
2 posts views Thread by Bala | last post: by
reply views Thread by just.starting | last post: by
3 posts views Thread by xeroxero | last post: by
1 post views Thread by =?Utf-8?B?U2FjaGluIENoYXZhbg==?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.