473,756 Members | 3,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,fi leName):
"""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?t sid=20070803-143313-49566ea2', 'xpersia4.3gp' )

Aug 3 '07 #1
7 2544
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,fi leName):
"""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?t sid=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,fi leName):
"""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,fi leName):
"""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?t sid=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...@holdenwe b.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,fi leName):
"""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?t sid=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...@holdenwe b.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...@holdenwe b.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
2164
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 href="mypath/myfile.py"> and not surprisingly, it displays like a webpage, but just the code. If I gzip it, and then link to the new file, it will download, but its so small I don't want it zipped. How can I make this into a downloadable file SIMPLY? The other...
0
1377
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 20% discount. Help with downloading large files
2
2436
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 impersonating the domain user account and downloading the file, It seems the impersonate is got success, but its not download the file. Its simply downloading some junk pdf file instead of original file.
1
2083
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 any page from the server while downloading. If I try to call a single page while downloading a file then the page request goes time out and the server then closes the existing download stream and the client doestn't throw any exception. So many a...
3
2791
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 any page from the server while downloading. If I try to call a single page while downloading a file then the page request goes time out and the server then closes the existing download stream and the client doestn't throw any exception. So many a...
0
1637
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 downloading with the error message: "the source file could not be read". And the error is not dependent on the size of the file, as I can download a file of 6MB but having problem with a file with 652KB. It always stops after 666880bytes. Thanks.
4
8703
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 within a certain size range, and I'm having to download all the files on the page, open them, get their size, and then only analyse the ones that are in that size range. Is there a way (in python, of course!) to get the size of images before or...
3
1723
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 on a button, the ASP.NET page will facilitate a download with Response.BinaryWrite. Is that possible? Thanks.
1
1379
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 adding n I need to download the recently uploaded files only each time my schedular runs ingnoring the previously downloaded files as there will be huge number of already downloaded files n downloading them again is not worth. How do I acheive this...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.