Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking the number of command line arguments in python

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)):
    print """\
This script will compare two files for something
and print out matches

Usage:  theScript firstfile secondfile
"""
    return

I want to do this at the beginning of the script to check for the right number of arguments. The problem is "return" doesn't work here. I could do some big if then statement I suppose, but was hoping to not have to do that. Not sure if there is any easier way. Any ideas?

like image 691
mdmenard Avatar asked Feb 18 '16 20:02

mdmenard


1 Answers

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)
like image 139
gariepy Avatar answered Sep 20 '22 18:09

gariepy