python采用getopt解析命令行输入参数实例
作者:shichen2014 时间:2022-05-10 11:08:00
本文实例讲述了python采用getopt解析命令行输入参数的方法,分享给大家供大家参考。
具体实例代码如下:
import getopt
import sys
config = {
"input":"",
"output":".",
}
#getopt三个选项,第一个一般为sys.argv[1:],第二个参数为短参数,如果参数后面必须跟值,须加:,第三个参数为长参数
#是一个列表,
opts, args = getopt.getopt(sys.argv[1:], 'hi:o:d',
[
'input=',
'output=',
'help'
]
)
#参数的解析过程,长参数为--,短参数为-
for option, value in opts:
if option in ["-h","--help"]:
print """
usage:%s --input=[value] --output=[value]
usage:%s -input value -o value
"""
elif option in ['--input', '-i']:
config["input"] = value
elif option in ['--output', '-o']:
config["output"] = value
elif option == "-d":
print "usage -d"
print config
输入的参数:
--input=c:\temp\aa -o c:\temp\output -d
打印的结果:
usage -d
{'input': 'c:\\temp\\aa', 'output': 'c:\\temp\\output'}
希望本文所述对大家的Python程序设计有所帮助。
标签:python,getopt
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用python实现excel的Vlookup功能
2023-05-01 20:15:15
![](https://img.aspxhome.com/file/2023/1/65511_0s.png)
在python中将list分段并保存为array类型的方法
2023-11-15 10:18:00
Go微服务项目配置文件的定义和读取示例详解
2023-06-23 22:48:50
![](https://img.aspxhome.com/file/2023/6/89016_0s.png)
msxml3.dll 错误 ‘800c0005’解决方案
2009-10-05 18:36:00
关于php开启错误提示的总结
2023-11-04 10:46:21
Python模块学习 filecmp 文件比较
2023-07-09 18:30:44
Python中ini配置文件读写的实现
2021-03-15 09:52:01
Python中py文件转换成exe可执行文件的方法
2022-09-30 02:01:40
![](https://img.aspxhome.com/file/2023/9/104809_0s.jpg)
安装多个版本的TensorFlow的方法步骤
2022-12-10 00:13:02
![](https://img.aspxhome.com/file/2023/3/93473_0s.png)
Mootools常用方法扩展(二)
2009-01-11 18:22:00
Python3.6 之后字典是有序的?
2021-02-14 08:27:53
![](https://img.aspxhome.com/file/2023/3/95283_0s.png)
Python实现 版本号对比功能的实例代码
2022-07-22 05:53:59
最基础的Python的socket编程入门教程
2022-10-13 03:38:46
自动在Windows中运行Python脚本并定时触发功能实现
2023-04-15 21:12:38
![](https://img.aspxhome.com/file/2023/6/76926_0s.png)
用Python监控你的朋友都在浏览哪些网站?
2022-10-20 00:37:07
![](https://img.aspxhome.com/file/2023/1/97221_0s.jpg)
Collatz 序列、逗号代码、字符图网格实例
2023-07-27 10:24:31
学习Python3 Dlib19.7进行人脸面部识别
2022-03-18 00:01:30
![](https://img.aspxhome.com/file/2023/5/76035_0s.png)
javascript同步服务器时间和同步倒计时小技巧
2023-08-27 22:10:58
![](https://img.aspxhome.com/file/2023/6/56116_0s.jpg)
理解Python数据离散化手写if-elif语句与pandas中cut()方法实现
2023-02-24 10:33:33
用python实现监控视频人数统计
2022-04-03 16:01:31
![](https://img.aspxhome.com/file/2023/8/75278_0s.png)