开发者

Run command line arguments in python script

开发者 https://www.devze.com 2023-01-20 05:36 出处:网络
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 开发者_StackOverflow社区of running it with just the \'100\' arg

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 开发者_StackOverflow社区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.


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消