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

yipeiwu_com7年前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 判断网络连通的实现方法

开发中偶尔需要判断网络的连通性,没有什么方法比 ping 更直接了当,通常检查网络情况都是运行命令ping www.baidu.com ,查看输出信息即可。 C:\Users>...

Python内置的字符串处理函数整理

str='python String function' 生成字符串变量str='python String function'字符串长度获取:len(str)例:print '%s l...

python strip()函数 介绍

描述 Python strip() 方法用于移除字符串头尾指定的字符(默认为空格)。 语法 strip()方法语法: str.strip([chars]); 参数 chars -- 移除...

python批量替换多文件字符串问题详解

系统如下: 操作系统 : CentOS7.3.1611_x64 Python 版本 : 2.7.5 问题描述 编码过程中有时候会遇到在多个源文件中存在同一个变量名(比如 : write...

python读写二进制文件的方法

本文实例讲述了python读写二进制文件的方法。分享给大家供大家参考。具体如下: 初学python,现在要读一个二进制文件,查找doc只发现 file提供了一个read和write函数,...