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学习之asyncore模块用法实例教程

本文以实例分析了Python中asyncore模块的原理及用法,分享给大家供大家参考。具体分析如下: asyncore库是python的一个标准库,它是一个异步socket的包装。我们操...

Python创建一个元素都为0的列表实例

实现创建一个元素都为0的列表: l=10 lis=[0]*l lis 输出结果: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 以上这篇Python创建一个元...

python sort、sorted高级排序技巧

Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列。 1)排序基础 简单的升序排序是非常容易的。只需要...

python实现图片变亮或者变暗的方法

python实现图片变亮或者变暗的方法

本文实例讲述了python实现图片变亮或者变暗的方法。分享给大家供大家参考。具体实现方法如下: import Image # open an image file (.jpg or....

PyQt5固定窗口大小的方法

PyQt5固定窗口大小的方法

直接以数值固定大小 根据屏幕大小固定大小 禁止最大化按钮 MainWindow.setWindowFlags(QtCore.Qt.WindowMinimizeButtonHint...