Python中的sys.argv[]用法

发布时间:2019-09-11 07:42:16编辑:auto阅读(1789)

    #!/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)

关键字