Python 仅在sys.argv[1]存在时执行代码

Python 仅在sys.argv[1]存在时执行代码,python,linux,if-statement,sys,Python,Linux,If Statement,Sys,我有以下代码: #!/usr/bin/python import os.path import sys if len(sys.argv)<2: print"You need to specify file!" if (os.path.isfile(sys.argv[1])): print "File <%s> exist" % sys.argv[1] elif (sys.argv[1] == "--help"): print "Add (only)one f

我有以下代码:

#!/usr/bin/python

import os.path
import sys
if len(sys.argv)<2:
   print"You need to specify file!" 
if (os.path.isfile(sys.argv[1])):
   print "File <%s> exist" % sys.argv[1]
elif (sys.argv[1] == "--help"):
  print "Add (only)one file argument to command"
  print "--help                   print this screen"
  print "--autor                  autor name and email adress"
  print "--about                  about this program"
elif (sys.argv[1] == "--about"):
  print"Program to identify if the file exists"
  print"Copyright Vojtech Horanek 2015"
elif (sys.argv[1] == "--autor"):
  print"Vojtech Horanek <vojtechhoranek@gmail.com>"
else:
  print"No file <%s> found" % sys.argv[1]
#/usr/bin/python
导入操作系统路径
导入系统

如果len(sys.argv)
if len(sys.argv)
if len(sys.argv)
if len(sys.argv)
if len(sys.argv)您可以检查len(sys.args[])>1您最好使用optpass来检查len(sys.args[])>1您最好使用optpass来检查len(sys.args[])>1您最好使用optpasse您可以检查len(sys.args[])>1您最好使用optpasse
if (os.path.isfile(sys.argv[1])):
   print "File <%s> exist" % sys.argv[1]
elif (sys.argv[1] == "--help"):
   print "Add (only)one file argument to command"
   print "--help                   print this screen"  
   print "--autor                  autor name and email adress"
   print "--about                  about this program"
elif (sys.argv[1] == "--about"):
   print"Program to identify if the file exists"
   print"Copyright Vojtech Horanek 2015"
elif (sys.argv[1] == "--autor"):
   print"Vojtech Horanek <vojtechhoranek@gmail.com>"
else:
  print"No file <%s> found" % sys.argv[1]
You need to specify file!
Traceback (most recent call last):
File "program.py", line 7, in <module>
if (os.path.isfile(sys.argv[1])):
IndexError: list index out of range
if len(sys.argv)<2:
   print"You need to specify file!" 
   sys.exit()