Python命令行参数argv和argparse该如何使用
作者:葛木瓜 时间:2022-04-05 01:38:52
概述
运行python脚本时通过命令行方式传入运行参数通常有以下两种自建方式:
sys.argv - 简洁
argparse - 丰富,可自定义
下面详细说一下具体时使用
argv
# test_argv.py
import sys
args = sys.argv
print(f'args = {args}')
>>> output
➜ git:(master) python3 test_argv.py
args = ['test_argv.py']
➜ git:(master) ✗ python3 test_argv.py 1 2 3
args = ['test_argv.py', '1', '2', '3']
➜ git:(master) ✗ python3 test_argv.py 1 2 3 'hello world !'
args = ['test_argv.py', '1', '2', '3', 'hello world !']
从上面可以看出,通过argv
方法获取的结果:
返回为list
第一个参数为脚本本身
如参数中间带空格,用引号即可
argparse
argparse模块的功能较为丰富,其核心是通过add_argument方法自定义入参的:标志、格式、类型和范围等特性,常用如下:
*name_or_flag - 定义入参名或flag,如'-n', '--number'
type - 指定入参类型
choices - 指定入参范围
default - 指定入参默认值
required - 指定该餐素是否不要,布尔类型
help - 参数概述
更多请参考: 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'>
来源:https://www.cnblogs.com/freedomlidi/p/12680005.html
标签:python,命令行参数,argv,argparse


猜你喜欢
关于SSD目标检测模型的人脸口罩识别
2023-06-20 05:20:56

Python批量对word文档进行操作步骤
2022-07-24 03:37:36

如何使用Django(python)实现android的服务器端
2022-09-25 01:06:43

远程登陆SQL Server 2014数据库的方法
2024-01-28 03:39:01

如何利用python turtle绘图自定义画布背景颜色
2021-08-02 17:28:49

源码解析python的内存回收机制
2023-05-19 18:12:16
vue $mount 和 el的区别说明
2024-04-28 09:20:24
微信公众平台开发——群发信息
2023-05-19 16:18:48

git 拉取远程分支到本地的方法步骤
2022-08-12 15:09:56

python实现根据给定坐标点生成多边形mask的例子
2022-03-22 14:40:15
php自定义函数call_user_func和call_user_func_array详解
2024-05-11 10:09:11
php生成随机数/生成随机字符串的方法小结【5种方法】
2023-09-05 20:23:21
Python机器学习利用鸢尾花数据绘制ROC和AUC曲线
2023-09-06 23:18:24

SQL2000个人版 应用程序正常初始化失败0乘以C0000135失败
2024-01-27 05:09:39
python str()如何将参数转换为字符串类型
2022-04-28 17:30:36
详解将Django部署到Centos7全攻略
2022-03-23 01:01:12

python创建临时文件夹的方法
2022-02-13 21:51:35
Python3单行定义多个变量或赋值方法
2022-03-22 17:10:53

在C#和MySQL中存取中文字符时避免乱码的方法
2024-01-15 13:59:02
iOS和Android用同一个二维码实现跳转下载链接的方法
2024-04-22 13:24:16