帝王谷资源网 Design By www.wdxyy.com
概述
运行python脚本时通过命令行方式传入运行参数通常有以下两种自建方式:
- sys.argv - 简洁
- argparse - 丰富,可自定义
下面详细说一下具体时使用
argv
# test_argv.py import sys args = sys.argv print(f'args = {args}') > output "_blank" href="https://docs.python.org/zh-cn/dev/library/argparse.html" rel="external nofollow" >argparse实例
test_argv.py
import argparse # 初始化一个parser对象 parser = argparse.ArgumentParser(description='test module of argparse') # 指定-n/--number的参数 # 类型为int # help为简短地说明 parser.add_argument( '-n', '--number', type=int, help='args of number' ) # 指定-o/--output参数 # 并限制类型为:['txt', 'csv', 'doc'] parser.add_argument( '-o', '--output', type=str, choices=['txt', 'csv', 'doc'], help='output method' ) # 指定-d/--default参数 # 并限制类型为:['txt', 'csv', 'doc'] parser.add_argument( '-d', '--default', type=int, choices=[_ for _ in range(1, 10)], default=5, help='default' ) # 指定位置参数foo parser.add_argument('foo') args = parser.parse_args() print(f'args = {args}') # 获取指定参数 print( f'number = {args.number}, type = {type(args.number)}\n' f'output = {args.output}, type = {type(args.output)}\n' f'default = {args.default}, type = {type(args.default)}\n' f'foo = {args.foo}, type = {type(args.foo)}' )output
# -h - 打印help ➜ git:(master) ✗ python3 test_argv.py -h usage: test_argv.py [-h] [-n NUMBER] [-o {txt,csv,doc}] [-d {1,2,3,4,5,6,7,8,9}] foo test module of argparse positional arguments: foo optional arguments: -h, --help show this help message and exit -n NUMBER, --number NUMBER args of number -o {txt,csv,doc}, --output {txt,csv,doc} output method -d {1,2,3,4,5,6,7,8,9}, --default {1,2,3,4,5,6,7,8,9} default # 不带参数运行,结果为None ➜ git:(master) ✗ python3 test_argv.py args = Namespace(number=None, output=None) number = None output = None # 带参数运行 ➜ git:(master) ✗ python3 test_argv.py -n 33 --output txt args = Namespace(number=33, output='txt') number = 33, type = <class 'int'> output = txt, type = <class 'str'> # 参数格式错误 ➜ git:(master) ✗ python3 test_argv.py -n str usage: test_argv.py [-h] [-n NUMBER] [-o {txt,csv,doc}] test_argv.py: error: argument -n/--number: invalid int value: 'str' ➜ git:(master) ✗ python3 test_argv.py -o excel usage: test_argv.py [-h] [-n NUMBER] [-o {txt,csv,doc}] test_argv.py: error: argument -o/--output: invalid choice: 'excel' (choose from 'txt', 'csv', 'doc') # 默认参数 ➜ git:(master) ✗ python3 test_argv.py args = Namespace(default=5, number=None, output=None) number = None, type = <class 'NoneType'> output = None, type = <class 'NoneType'> output = 5, type = <class 'int'>以上就是Python命令行参数argv和argparse该如何使用的详细内容,更多关于Python命令行参数argv和argparse的资料请关注其它相关文章!
帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com
暂无评论...
更新日志
2024年10月25日
2024年10月25日
- 群星2013-青春缤纷辑压箱宝大公开3CD2[新加坡限量版][WAV整轨]
- 林育群.2013-BalladShow(日本版)【环球】【WAV+CUE】
- 陈加洛.1992-痛到感觉不到【宝丽金】【WAV+CUE】
- 群星.2023-宿命之敌电视剧原声带【韶愔音乐】【FLAC分轨】
- 東京事変-大発見[FLAC+CUE]
- 椎名林檎-三文ゴシップ[FLAC+CUE]
- 2024年08月04日
- 裘德《裘德「最后的水族馆」演唱会LIVE》[320K/MP3][228.89MB]
- 裘德《裘德「最后的水族馆」演唱会LIVE》[24bit 48kHz][FLAC/分轨][2.08G]
- 基因三重奏《如果你什么都不说 音乐会现场录音》[320K/MP3][145.37MB]
- 孟庭苇.1996-月亮说话(2020环球24KGOLD限量版)【上华】【WAV+CUE】
- 群星.1997-新艺宝优质音响系列·国语精选监听版【新艺宝】【WAV+CUE】
- 阿桑.2005-寂寞在唱歌(星外星引进版)【华研国际】【WAV+CUE】
- 基因三重奏《如果你什么都不说 音乐会现场录音》[FLAC/分轨][287.43MB]
- 蔡题谦《我爱你,却依然要看你走》[320K/MP3][88.65MB]