zoukankan      html  css  js  c++  java
  • Python3 中使用sys.argv详解

    #/usr/bin/env python
    #coding:utf-8
    
    import sys
    # print(sys.argv[1])
    def readFile(filename):
        """定义readFile函数,从文件中读出文件内容"""
        with open(filename) as f:
            while True:
                line = f.readline()
                if len(line) == 0:
                    break
                print(line)
    
    print(sys.argv)
    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,索引为2开始,往后取所有
        if option == 'version':#当命令行参数为-- version,显示版本号
            print('version 1.2')
        elif option == 'help': #当命令行参数为--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('Unknow option')
        sys.exit()
    
    
    else:
        for filename in sys.argv[1:]:#当参数为文件名时,传入readfile,读出其内容
            readFile(filename)
    
  • 相关阅读:
    监听
    用户管理
    oracle网络
    实例 参数
    存储管理
    oracle 体系
    实例
    修改
    集合操作
    17.08.18
  • 原文地址:https://www.cnblogs.com/skymyyang/p/7326137.html
Copyright © 2011-2022 走看看