473,382 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 2522
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Luke StClair | last post by:
Only marginally belonging in this newsgroup... but oh well. I've just started writing in python, and I want to make the files available on the web. So I did the standard <a...
0
by: sales | last post by:
If you are having difficulty downloading large files, please check out www.Downloads4Dialups.com. Mention this newsgroup in the "Special Instructions" window on the Shipping Form, and receive a...
2
by: Bala | last post by:
Hi I am trying to download the PDF files from my webserver using ASP.Net. All my files are stored at F Drive on webserver. Like this F:\Main Folder\Sub Folder\Files\File1.pdf I am...
1
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
3
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
0
by: just.starting | last post by:
I am having problem while downloading files from an apache server2.0.53 with php4.3.10.While downloading some files it generally stops after downloading some specific amount and then stops...
4
by: aldonnelley | last post by:
Hi there: a bit of a left-field question, I think. I'm writing a program that analyses image files downloaded with a basic crawler, and it's slow, mainly because I only want to analyse files...
3
by: xeroxero | last post by:
I would like to prevent people from downloading a .zip from a ASP.NET 2.0 web site, but still allow people to touch a .aspx page in the same directory. I also want to set things so if a user clicks...
1
by: =?Utf-8?B?U2FjaGluIENoYXZhbg==?= | last post by:
Hi, I need to download files from the FTP location. This FTP location is readonly n I dont hv access to delete files from this location once downloaded. The files to the ftp location keeps...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.