5

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
  • Do you want multiple arguments or a single argument that is a python list? Commented Oct 15, 2010 at 3:06
  • Added the [shell] and [shell-scripting] tags; not sure if this is what the OP has in mind, but it seems like the most obvious approach to at least a couple of people. Commented Oct 15, 2010 at 3:37

4 Answers 4

8

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
actually, change it to "$@" so you can do the_new_script 1 2 "3 4" 5
6

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.

2 Comments

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, 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?)
4

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.

1 Comment

Note: As of Python 2.7 Optparse has been deprecated and replaced by the nearly identical argparse.
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.