首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python运行有关问题

2012-03-04 
python运行问题import sysdef readfile(filename):Print a file to the standard output.f open(f

python运行问题
import sys

def readfile(filename):
  '''Print a file to the standard output.'''
  f = open(filename)
  while True:
  line = f.readline()
  if len(line) == 0:
  break
  print (line), # notice comma
  f.close()

# Script starts from here
if len(sys.argv) < 2:
  print ('No action specified.')
  sys.exit

if sys.argv[1].startswith('--'):
  option = sys.argv[1][2:]
  # fetch sys.argv[1] but without the first two characters
  if option == 'version':
  print ('Version 1.2')
  elif option == 'help':
  print ('''\
This program prints files to the standard output.
Any number of files can be specified.
Options include:
  --version : Prints the version number
  --help : Display this help''')
  else:
  print ('Unknown option.')
  sys.exit
else:
  for filename in sys.argv[1:]:
  readfile(filename) 


问题:
No action specified.
Traceback (most recent call last):
  File "C:/Documents and Settings/Administrator/桌面/python test/cat.py", line 18, in <module>
  if sys.argv[1].startswith('--'):
IndexError: list index out of range

[解决办法]
lz 显然没有在执行程序的时候,提供一个(命令行)参数
[解决办法]
sys.argv[1]:越界

热点排行