Python中的sys.argv[]用法

2023-01-31 05:01:19 python sys 用法

#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys

def readFile(filename):
    f = file(filename)
    while True:
        fileContext = f.readline()
        if len(fileContext) ==0:
            break;
        print fileContext
    f.close()

if len(sys.argv) < 2:
    print "No function be setted."
    sys.exit()
if sys.argv[1].startswith("-"):
    option = sys.argv[1][1:]
    if option == 'version':
        print "Version1.2"
    elif option == 'help':
        print "enter an filename to see the context of it!"
    else:
        print "Unknown function!"
        sys.exit()
else:
    for filename in sys.argv[1:]:
        readFile(filename)

相关文章