473,387 Members | 1,619 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,387 software developers and data experts.

Need script to download file at known address

I am fairly new to the latest verion of Python and using it on windows
95, 2000, and/or XP. What libraries, modules, functions, etc. would I
need to set up a Python script to download a file, say
"htttp://www.sound.com/files/bob.wav" to my own hard drive at
"c:\sound\bob.wav"?

I haven't found any good examples of such an operation in the
documentation at the Python website. Any suggestions are appreciated.
Thanks.
Jul 18 '05 #1
8 6540
On Wed, 15 Sep 2004 00:00:21 GMT Radioactive wrote:
I am fairly new to the latest verion of Python and using it on windows
95, 2000, and/or XP. What libraries, modules, functions, etc. would I
need to set up a Python script to download a file, say
"htttp://www.sound.com/files/bob.wav" to my own hard drive at
"c:\sound\bob.wav"?

I haven't found any good examples of such an operation in the
documentation at the Python website. Any suggestions are appreciated.
Thanks.

See: 11. Internet Protocols and Support
at http://python.org/doc/2.3.4/lib/lib.html

In this example, Yahoo uses 00=Jan, so 08=Sep...
from urllib import urlopen
URL1 = "http://ichart.finance.yahoo.com/table.csv?"
URL2 = "s=IBM&a=08&b=14&c=2004&d=08&e=14&f=2004&g=d&ignor e=.csv"
urlopen("%s%s" % (URL1,URL2)).readlines() ['Date,Open,High,Low,Close,Volume,Adj. Close*\n',
'14-Sep-04,86.60,86.88,86.15,86.72,3953500,86.72\n', '<!--
ichart9.finance.dcn.yahoo.com uncompressed Tue Sep 14 17:55:33 PDT 2004
-->\n']

Jul 18 '05 #2
Radioactive Man <rm@rm.rm> wrote:
I am fairly new to the latest verion of Python and using it on windows
95, 2000, and/or XP. What libraries, modules, functions, etc. would I
need to set up a Python script to download a file, say
"htttp://www.sound.com/files/bob.wav" to my own hard drive at
"c:\sound\bob.wav"?
Something like:

import urllib
urllib.urlretrieve( "htttp://www.sound.com/files/bob.wav",
'c:/sound/bob.wav')

should be fine.
I haven't found any good examples of such an operation in the
documentation at the Python website. Any suggestions are appreciated.


http://docs.python.org/lib/module-urllib.html has the docs of
urlretrieve, and it's SO simple that I don't really see what "good
examples" one might provide. A book that by design is full of examples
(hopefully good) is the Python Cookbook, which in the intro to Chapter
10 (Network Programming) gives a slightly richer example which downloads
several files with urllib.urlretrieve.
http://www.python9.org/p9-zadka.ppt may have some useful pointers,
perhaps.

http://www.experts-exchange.com/Prog...nguages/Python
/Q_21048439.html has exactly the same question you asked, but you have
to "sign up" to see the solution (always the same one).
Alex
Jul 18 '05 #3
On Wed, 15 Sep 2004 17:20:43 +0200, al*****@yahoo.com (Alex Martelli)
wrote:
Radioactive Man <rm@rm.rm> wrote:
I am fairly new to the latest verion of Python and using it on windows
95, 2000, and/or XP. What libraries, modules, functions, etc. would I
need to set up a Python script to download a file, say
"htttp://www.sound.com/files/bob.wav" to my own hard drive at
"c:\sound\bob.wav"?


Something like:

import urllib
urllib.urlretrieve( "htttp://www.sound.com/files/bob.wav",
'c:/sound/bob.wav')

should be fine.
I haven't found any good examples of such an operation in the
documentation at the Python website. Any suggestions are appreciated.


http://docs.python.org/lib/module-urllib.html has the docs of
urlretrieve, and it's SO simple that I don't really see what "good
examples" one might provide. A book that by design is full of examples
(hopefully good) is the Python Cookbook, which in the intro to Chapter
10 (Network Programming) gives a slightly richer example which downloads
several files with urllib.urlretrieve.
http://www.python9.org/p9-zadka.ppt may have some useful pointers,
perhaps.

http://www.experts-exchange.com/Prog...nguages/Python
/Q_21048439.html has exactly the same question you asked, but you have
to "sign up" to see the solution (always the same one).
Alex

Thanks to all who replied on this one. I have managed to download,
but somehow the file is getting mangled when I save it to my hard
drive. Here is the test script I came up with:

import urllib
f = urllib.urlopen("http://www.python.org/pics/pythonHi.gif")
g = f.read()
file = open("trash.gif", "w")
file.write(g)
file.close()

The file "trash.gif" is actually saved under "C:\Python23" in the
correct format (recognizable to *.gif viewing programs), but is so
severely mangled that I can't even recognize it visually. It happens
every time, so I do not believe it is a random event. My question
here is what am I doing wrong and at what stage is the file getting
mangled? I know this method works fine with text files, but for some
reason is damaging to binary files.

The same result happened when I substituted urllib.urlretrieve() for
urllib.urlopen().
Jul 18 '05 #4
On Fri, 17 Sep 2004 01:46:42 GMT, Radioactive Man <rm@rm.rm> wrote:
Here is the test script I came up with:

import urllib
f = urllib.urlopen("http://www.python.org/pics/pythonHi.gif")
g = f.read()
file = open("trash.gif", "w")
file.write(g)
file.close()


You're opening the output file in ascii mode, so newlines characters
are getting converted to the CR/LF combination. Change the second
parameter to open() to "wb".
Jul 18 '05 #5
On Fri, 17 Sep 2004 12:08:36 +1000, Andrew Durdin <ad*****@gmail.com>
wrote:
On Fri, 17 Sep 2004 01:46:42 GMT, Radioactive Man <rm@rm.rm> wrote:
Here is the test script I came up with:

import urllib
f = urllib.urlopen("http://www.python.org/pics/pythonHi.gif")
g = f.read()
file = open("trash.gif", "w")
file.write(g)
file.close()


You're opening the output file in ascii mode, so newlines characters
are getting converted to the CR/LF combination. Change the second
parameter to open() to "wb".


That worked. Thanks.

Looking back through the documentation, I see that open() is really an
alias for file(). Are there any variants of the "open" commmand that
allow location (drive and directory) of the file to be specified as
well, for example, if I wanted to save the file as
"D:\binaries\trash.gif" instead of the default location? If that's
not possible, then the alternative might be exectuting commands from
the os module to accomplish the same thing.

Jul 18 '05 #6
Radioactive Man wrote:
Looking back through the documentation, I see that open() is really an
alias for file().

For now... but (despite what those docs imply) it's really best to stick
with using open() and don't worry about the existence of file() unless
you're trying to subclass it... (GvR has recently said that he never
intended for file() to replace open(), and that the direct use of file()
is not preferred... which surprised a lot of us, apparently including
the person who wrote those docs. :) )
Are there any variants of the "open" commmand that
allow location (drive and directory) of the file to be specified as
well, for example, if I wanted to save the file as
"D:\binaries\trash.gif" instead of the default location?


As a matter of fact, the standard open() will handle that just fine --
it accepts a pathname, rather than just a filename, so you can feed it
an absolute path like your example or a relative path (e.g.,
subdir\trash.gif or ..\siblingdir\trash.gif) and it'll be perfectly
happy. The one catch is that, on Windows, the directory separator
conflicts with the escape character, '\'. So when typing path literals,
be sure to either always use double backslashes (
"d:\\binaries\\trash.gif" ) or 'raw' strings ( r"d:\binaries\trash.gif"
) so that your \trash doesnt become [tab]rash. Or better yet, use
os.path.join() (and the other os.path functions) to construct your paths.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #7
On Thu, 16 Sep 2004 19:59:17 -0700, Jeff Shannon <je**@ccvcorp.com>
declaimed the following in comp.lang.python:
happy. The one catch is that, on Windows, the directory separator
conflicts with the escape character, '\'. So when typing path literals,
be sure to either always use double backslashes (
"d:\\binaries\\trash.gif" ) or 'raw' strings ( r"d:\binaries\trash.gif"
) so that your \trash doesnt become [tab]rash. Or better yet, use
os.path.join() (and the other os.path functions) to construct your paths.
When used in direct programming calls (like open() ), Windows
seems happy with "/"s. It is only in things like os.system() where the
string is passed to an external command interpreter that you MUST use
the "\"s.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #8
Jeff Shannon wrote:
Radioactive Man wrote:
Are there any variants of the "open" commmand that
allow location (drive and directory) of the file to be specified as
well, for example, if I wanted to save the file as
"D:\binaries\trash.gif" instead of the default location?


As a matter of fact, the standard open() will handle that just fine --
it accepts a pathname, rather than just a filename, so you can feed it
an absolute path like your example or a relative path ...


But note that the directories have to exist already. If they don't,
you need to use something like os.makedirs() to get 'em.

-Peter
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: S P Arif Sahari Wibowo | last post by:
Hi! I am thinking to have a client-side script doing processing on file downloading, basically the script will process a downloaded file from the server before it received by the user. For...
4
by: Kevin Muenzler, WB5RUE | last post by:
How do I force a browser to download a file instead of displaying it? In other words I have a page with MP3 and WMA files on it and I would like for the visitor to download the file instead of...
0
by: mizi | last post by:
I am writing an ASP page for downloading some files, such as jpg, doc which are known MIME type. I want to force user save these files through a "file download" dialog instead of open them on the...
7
by: clintonG | last post by:
To all Microsoft partners and customers who have been unable to download recently or access ASP.NET documentation from the msdn2 website and for all of those customers who have been lied to and...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
1
by: michael.buonomo | last post by:
We have been using the Google recommended python script for about a year. We recently realized that the script was not crawling our sites url's, but just our folders which reside on the server. The...
6
by: Bob | last post by:
Hello everyone !!! I have a very neat script to download files to the server, the problem is that it uploads all kind of files, txt, exe, zip, you name it. I have been trying to add some code but...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
1
by: Tim Jones | last post by:
I have a web site where we offer MP3 downloads (yes, they are legal!). I've written a PHP script using readfile() (or fpassthru()) that sends the file using HTTP headers (via various header()...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...

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.