由于忙着论文,加上自己是小白,所以这个程序调试耽搁了很久。
sys模块包含系统对应的功能,它包含命令行参数。
如何打开python的命令窗口:按住shift键,然后鼠标右键在此此处打开命令窗口。
#!/usr/bin/python
#Filename:cat.py
import sys
def readfile(filename):
'''Print a file to the standard output.'''
f=file(filename)
while True:
line=f.readline()
if len(line)==0:
break
print line,
f.close() %注意这行要和while行顶齐,不然读不出poem%
if len(sys.argv)<2:
print'No action specified.'
sys.exit()
if sys.argv[1].startswith('--'):
option=sys.argv[1][2:]
if option=='version':
print'Version1.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)