views:

113

answers:

4

I have a program that is run from the command line like this

python program.py 100 rfile

How can I write a new script so that instead of running it with just the '100' argument, I can run it consecutively with a list of arguments like [50, 100, 150, 200]?

Edit: The reason I am asking is that I want to record how 'program.py' executes with different arguments.

+2  A: 

You can use Devrim's shell approach or you can modify your script:

If your original script worked like this:

import sys
do_something(sys.argv[1], sys.argv[2])

You could accomplish what you want like this:

def main(args):
    for arg in args[:-1]:
        do_something(arg, args[-1])

if __name__ == '__main__':
    import sys
    main(sys.argv[1:])

You would invoke it like so:

python program.py 50 100 150 200 rfile

I'm guessing at what you want. Please clarify if this isn't right.

Jon-Eric
I may not have explained what I wanted to do very well. I do not want to modify the 'program.py' file I originally mentioned. I want to create a new script that runs the 'program.py' script multiple times on a list of arguments, using one argument each time.
Chris
@Chris, it might be easiest to use Devrim's shell approach. It's easy to call functions in other python files, but not quite as easy if program.py doesn't have something like a "main function". Do you know what I mean? (Are you familiar with Python?)
Jon-Eric
+5  A: 

If you create a bash file like this

#!/bin/bash
for i in 1 2 3 4 5
do
  python program.py $i rfile
done

then do chmod +x on that file, when you run it, it will run these consecutively:

python program.py 1 rfile
python program.py 2 rfile
python program.py 3 rfile
python program.py 4 rfile
python program.py 5 rfile
Devrim
If you change `1 2 3 4 5` to `$@`, you can pass the list of numbers when you run the script, e.g. `the_new_bash_script 1 2 3 4 5`.
intuited
actually, change it to `"$@"` so you can do `the_new_script 1 2 "3 4" 5`
glenn jackman
+2  A: 

You can use Optparse

It lets you use your program like : python yourcode.py --input input.txt --output output.txt

This is great when you have a number of console arguments.

So in your case you could do something like:

parser.add_option("-i","--input", action="store", type="int", nargs=4, dest="mylist")

Now in your console you can type in python program.py -i 50 100 150 200

In order to access the inputs you can use mylist as a list.

Nandit Tiku
+1  A: 

If you want to do this in your python script, you can arrange for it to take a list of integers by using the argparse module (untested code):

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('rfile', type=argparse.FileType('r'))
parser.add_argument('numbers', nargs='+', type=int)
ns = parser.parse_args()

Such activities are usually conducted under the auspices of a main function as explained by Jon-Eric.

If you call the resulting script from the shell with

python the_script.py filename 1 2 3 4 5 6

you will end up with ns.file being equal to the file filename, opened for read access, and ns.numbers being equal to the list [1, 2, 3, 4, 5, 6].

argparse is totally awesome and even prints out usage information for the script and its options if you call it with --help. You can customize it in far too many ways to explain here, just read the docs.

argparse is in the standard library as of Python 2.7; for earlier pythons it can be installed as a module in the normal way, e.g. via easy_install argparse, or by virtue of being a dependency of the package that your script is part of.

intuited