小編給大家分享一下Python命令行參數(shù)argv和argparse的使用方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

運(yùn)行python腳本時(shí)通過命令行方式傳入運(yùn)行參數(shù)通常有以下兩種自建方式:
sys.argv - 簡潔
argparse - 豐富,可自定義
下面詳細(xì)說一下具體時(shí)使用
# 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方法獲取的結(jié)果:
返回為list
第一個(gè)參數(shù)為腳本本身
如參數(shù)中間帶空格,用引號(hào)即可
argparse模塊的功能較為豐富,其核心是通過add_argument方法自定義入?yún)⒌模簶?biāo)志、格式、類型和范圍等特性,常用如下:
*name_or_flag - 定義入?yún)⒚騠lag,如'-n', '--number'
type - 指定入?yún)㈩愋?/p>
choices - 指定入?yún)⒎秶?/p>
default - 指定入?yún)⒛J(rèn)值
required - 指定該餐素是否不要,布爾類型
help - 參數(shù)概述
更多請(qǐng)參考: argparse
test_argv.py
import argparse
# 初始化一個(gè)parser對(duì)象
parser = argparse.ArgumentParser(description='test module of argparse')
# 指定-n/--number的參數(shù)
# 類型為int
# help為簡短地說明
parser.add_argument(
'-n', '--number', type=int,
help='args of number'
)
# 指定-o/--output參數(shù)
# 并限制類型為:['txt', 'csv', 'doc']
parser.add_argument(
'-o', '--output', type=str,
choices=['txt', 'csv', 'doc'],
help='output method'
)
# 指定-d/--default參數(shù)
# 并限制類型為:['txt', 'csv', 'doc']
parser.add_argument(
'-d', '--default', type=int,
choices=[_ for _ in range(1, 10)],
default=5,
help='default'
)
# 指定位置參數(shù)foo
parser.add_argument('foo')
args = parser.parse_args()
print(f'args = {args}')
# 獲取指定參數(shù)
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
# 不帶參數(shù)運(yùn)行,結(jié)果為None
? git:(master) ? python3 test_argv.py
args = Namespace(number=None, output=None)
number = None
output = None
# 帶參數(shù)運(yùn)行
? 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'>
# 參數(shù)格式錯(cuò)誤
? 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')
# 默認(rèn)參數(shù)
? 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'>看完了這篇文章,相信你對(duì)“Python命令行參數(shù)argv和argparse的使用方法”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
本文名稱:Python命令行參數(shù)argv和argparse的使用方法-創(chuàng)新互聯(lián)
瀏覽地址:http://chinadenli.net/article10/hhido.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、虛擬主機(jī)、搜索引擎優(yōu)化、企業(yè)網(wǎng)站制作、網(wǎng)站排名、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容