python 执行文件时额外参数获取的实例

yipeiwu_com6年前Python基础

如下所示:

def usage():
  print(' * usage:')
  print(' *  -c [val] : exporter_conf filepath, default importer_conf.')
  print(' *  -h : print this.')
  print(' *  -z : 不需要确认参数,直接执行')


do_not_confirm = False

conf = ''
#c: [c+冒号表示-c 后面有参数,hz表示-h,-z后面没参数,如果此时在-h 100加上参数,那么这个100的值是获取不到的] 
opts, args = getopt.getopt(sys.argv[1:], 'c:hz') 
for op, value in opts:
  value = value.replace('\'', '').replace('\"', '')
  if op == '-c':
    conf = value
  elif op == '-h':
    usage()
    sys.exit()
  elif op == '-z':
    do_not_confirm = True

以上这篇python 执行文件时额外参数获取的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

实例讲解Python中函数的调用与定义

调用函数: #!/usr/bin/env python3 # -*- coding: utf-8 -*- # 函数调用 >>> abs(100) 100...

python里运用私有属性和方法总结

python里运用私有属性和方法总结

如何在PYTHON里运用私有属性和方法 class File: def __init__(self, name): self.name = name sel...

基于sklearn实现Bagging算法(python)

基于sklearn实现Bagging算法(python)

本文使用的数据类型是数值型,每一个样本6个特征表示,所用的数据如图所示: 图中A,B,C,D,E,F列表示六个特征,G表示样本标签。每一行数据即为一个样本的六个特征和标签。 实现Bag...

Python库urllib与urllib2主要区别分析

作为一个Python菜鸟,之前一直懵懂于urllib和urllib2,以为2是1的升级版。今天看到老外写的一篇《Python: difference between urllib and...

对Python中一维向量和一维向量转置相乘的方法详解

对Python中一维向量和一维向量转置相乘的方法详解

在Python中有时会碰到需要一个一维列向量(n*1)与另一个一维列向量(n*1)的转置(1*n)相乘,得到一个n*n的矩阵的情况。但是在python中, 我们发现,无论是“.T”还是...