Checking the number of command line arguments in python

前端 未结 2 2282
忘掉有多难
忘掉有多难 2021-02-20 10:24

I\'m new to python. Still getting my feet wet. I\'m trying to do something like this:

import sys

if ((len(sys.argv) < 3 or < len(sys.argv > 3)):
             


        
相关标签:
2条回答
  • 2021-02-20 11:01

    Use sys.exit() to exit from a script.

    import sys
    
    if len(sys.argv) != 3:
        print """\
    This script will compare two files for something
    and print out matches
    
    Usage:  theScript firstfile secondfile
    """
        sys.exit(0)
    
    0 讨论(0)
  • 2021-02-20 11:14

    sys has a function called exit:

    sys.exit(1)
    

    is probably what you want. Using 1 tells the program calling your program that there was an error. You should probably consider writing to sys.stderr the reason for the error.

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