views:

52

answers:

2

I need a python script that will do the following:

  1. connect to a URL, and that URL will return a number like 1200.

  2. Use the number, to download xml files named: 1 to x where x is the number from #1.

  3. store the files in a particular directory.

Sorry I've never written a python script, so if you could guide me along that would be great (maybe with a some comments).

I will be running this as a cron job if that matters.

+2  A: 

Example using urllib:

import urllib
import os

URL = 'http://someurl.com/foo/bar'
DIRECTORY = '/some/local/folder'

# connect to a URL, and that URL will return a number like 1200.
number = int(urllib.urlopen(URL).read())

# Use the number, to download xml files named: 
# 1 to x where x is the number from #1.
# store the files in a particular directory.
for n in xrange(1, number + 1):
    filename = '%d.xml' % (n,)
    destination = os.path.join(DIRECTORY, filename)
    urllib2.urlretrieve(URL + '/' + filename, destination)
nosklo
A: 

If you're never written a python script, you would do better to look for a python tutorial first.

Once you have a small grasp of things, check out

http://docs.python.org/library/

For question #1, you'll want to look at

http://docs.python.org/library/internet.html

For question #2, you can do something like

max = 10 # assume from #1
for x in range(1, max+1):
    filename = 'some_file-' + str(x) + '.xml'
    # download the file - see above url for internet protocols
    # see http://docs.python.org/library/stdtypes.html#file-objects
    # for help on files

This question is very vague on details and while it doesn't smell like a homework assignment, it would not be a good idea to do it in a language you don't know at all, in particular if you are running it in cron.

Dave
its "homework" alright, but not for school but work! Well its actually for me own website, but its still work. too old for school :)
Blankman