[python] Does Python have an argc argument?

I have written the same program (open text file and display contents) in C and C++. Now am doing the same in Python (on a Linux machine).

In the C programs I used the code:

if (argc != 2) {
    /* exit program */
}

Question: What is used in Python to check the number of arguments

#!/usr/bin/python
import sys
try:
    in_file = open(sys.argv[1], "r")
except:
    sys.exit("ERROR. Did you make a mistake in the spelling")
text = in_file.read()
print text
in_file.close()

Current output:

./python names.txt = Displays text file (correct)
./python nam = error message: stated from the sys.ext line (correct)
./python = error message: stated from the sys.ext line (wrong: want it to be a
separate error message stating *no file name input*)

This question is related to python linux file-io error-handling arguments

The answer is


In python a list knows its length, so you can just do len(sys.argv) to get the number of elements in argv.


Similar questions with python tag:

Similar questions with linux tag:

Similar questions with file-io tag:

Similar questions with error-handling tag:

Similar questions with arguments tag: