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.
-
Do you want multiple arguments or a single argument that is a python list?Jon-Eric– Jon-Eric2010年10月15日 03:06:28 +00:00Commented 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.intuited– intuited2010年10月15日 03:37:14 +00:00Commented Oct 15, 2010 at 3:37
4 Answers 4
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
2 Comments
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."$@" so you can do the_new_script 1 2 "3 4" 5You 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
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
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.