Pass arguments from cmd to python script

后端 未结 3 1004
轮回少年
轮回少年 2021-01-02 18:31

I write my scripts in python and run them with cmd by typing in:

C:\\> python script.py

Some of my scripts contain separate algorithms a

相关标签:
3条回答
  • 2021-01-02 19:03

    It might not answer your question, but some people might find it usefull (I was looking for this here):

    How to send 2 args (arg1 + arg2) from cmd to python 3:

    ----- Send the args in test.cmd:

    python "C:\Users\test.pyw" "arg1" "arg2"
    

    ----- Retrieve the args in test.py:

    print ("This is the name of the script= ", sys.argv[0])
    print("Number of arguments= ", len(sys.argv))
    print("all args= ", str(sys.argv))
    print("arg1= ", sys.argv[1])
    print("arg2= ", sys.argv[2])
    
    0 讨论(0)
  • 2021-01-02 19:10

    There are a few modules specialized in parsing command line arguments: getopt, optparse and argparse. optparse is deprecated, and getopt is less powerful than argparse, so I advise you to use the latter, it'll be more helpful in the long run.

    Here's a short example:

    import argparse
    
    # Define the parser
    parser = argparse.ArgumentParser(description='Short sample app')
    
    # Declare an argument (`--algo`), saying that the 
    # corresponding value should be stored in the `algo` 
    # field, and using a default value if the argument 
    # isn't given
    parser.add_argument('--algo', action="store", dest='algo', default=0)
    
    # Now, parse the command line arguments and store the 
    # values in the `args` variable
    args = parser.parse_args()
    
    # Individual arguments can be accessed as attributes...
    print args.algo
    

    That should get you started. At worst, there's plenty of documentation available on line (say, this one for example)...

    0 讨论(0)
  • 2021-01-02 19:14

    Try using the getopt module. It can handle both short and long command line options and is implemented in a similar way in other languages (C, shell scripting, etc):

    import sys, getopt
    
    
    def main(argv):
    
        # default algorithm:
        algorithm = 1
    
        # parse command line options:
        try:
           opts, args = getopt.getopt(argv,"a:",["algorithm="])
        except getopt.GetoptError:
           <print usage>
           sys.exit(2)
    
        for opt, arg in opts:
           if opt in ("-a", "--algorithm"):
              # use alternative algorithm:
              algorithm = arg
    
        print "Using algorithm: ", algorithm
    
        # Positional command line arguments (i.e. non optional ones) are
        # still available via 'args':
        print "Positional args: ", args
    
    if __name__ == "__main__":
       main(sys.argv[1:])
    

    You can then pass specify a different algorithm by using the -a or --algorithm= options:

    python <scriptname> -a2               # use algorithm 2
    python <scriptname> --algorithm=2    # ditto
    

    See: getopt documentation

    0 讨论(0)
提交回复
热议问题