我已经用C和C ++编写了相同的程序(打开文本文件并显示内容)。现在在Linux机器上用Python做同样的事情。
在C程序中,我使用了代码:
if (argc != 2) {
/* exit program */
}
问题:Python用什么来检查参数数量
#!/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()
电流输出:
./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*)