Python 脚本接收命令行参数的多种方式 All In One
Python 脚本接收命令行参数的多种方式 All In One
-
sys
-
argparse
-
shell script
-
tensorflow
-
getopt
...
sys
#!/usr/bin/env python3
# coding: utf8
import sys
args = sys.argv
print("args length:", len(args))
print("args type:", type(args))
print("function name:", args[0])
print("first arg =", args[1])
print("second arg =", sys.argv[2])
https://docs.python.org/3/library/sys.html
sys
--- 系统相关的参数和函数¶
https://docs.python.org/zh-cn/3/library/sys.html
argparse
import argparse
parser = argparse.ArgumentParser(description='argparse testing')
parser.add_argument('--name','-n',type=str, default = "bk",required=True,help="a programmer's name")
parser.add_argument('--age','-a',type=int, default=35,help='age of the programmer')
parser.add_argument('--sex','-s',type=str, default='male')
parser.add_argument('--favorite','-f',type=str, nargs="+",required=False,help="favorite of the programmer")
args = parser.parse_args()
print(args.name)
print(args.age)
print(args.sex)
print(args.favorite)
https://docs.python.org/3/library/argparse.html
argparse
--- 命令行选项、参数和子命令解析器
https://docs.python.org/zh-cn/3/library/argparse.html
shell script
# 定义参数
$ ./args.sh arg1 arg2
#!/usr/bin/env bash
# 接收参数
arg1=$1
arg2=$2
# 使用参数
python3 ./test.py $arg1 $arg2
tensorflow FLAGS
$ python script.py -gpus=0,1,2 --batch_size=10
import tensorflow as tf
tf.app.flags.DEFINE_string('gpus', None, 'gpus to use')
tf.app.flags.DEFINE_integer('batch_size', 5, 'batch size')
FLAGS = tf.app.flags.FLAGS
def main(_):
print FLAGS.gpus
print FLAGS.batch_size
if __name__=="__main__":
tf.app.run()
__future__
Python 3 兼容 Python 2.x 的方案项目包
"""Implementation of the flags interface."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse as _argparse
from tensorflow.python.util.all_util import remove_undocumented
_global_parser = _argparse.ArgumentParser()
# pylint: disable=invalid-name
https://github.com/tensorflow/tensorflow/blob/r1.2/tensorflow/python/platform/flags.py
https://tensorflow.google.cn/?hl=zh-cn
demos
sys
#!/usr/bin/python3
# coding: utf8
import RPi.GPIO as GPIO
import time
import sys
arg1 = sys.argv[1]
print("arg1 =", arg1);
# 指定 BCM 模式下的GPIO 针脚编号
PIN = 12
# BCM 模式
GPIO.setmode(GPIO.BCM)
# 指定 GPIO 针脚为一个电流输出针脚
GPIO.setup(PIN, GPIO.OUT)
# 输出低电平
GPIO.output(PIN, GPIO.LOW)
# index
i = 0
# max
# n = 7
# 类型转换,str => int
n = int(arg1)
print("n =", n)
print('开始闪烁⏳')
while (i < n):
print("i =", i)
# 高电平,LED 点亮
GPIO.output(PIN, GPIO.HIGH)
# 休眠 1 秒,防止 LED 长时间点亮烧坏了
time.sleep(1.0)
# 低电平,LED 熄灭
GPIO.output(PIN, GPIO.LOW)
# 休眠 1 秒
time.sleep(1.0)
i = i + 1
# 输出低电平,LED 关闭
# GPIO.output(PIN, GPIO.LOW)
# 清除,释放内存
GPIO.cleanup()
print('结束闪烁 👌🏻')
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
Python 3
vs Python 2
Python3.x 与 Python2.x 版本区别
如果 Python2.x 版本想使用使用 Python3.x 的 print 函数,可以导入 __future__
包;
该包禁用
Python2.x 的 print 语句
,采用 Python3.x 的 print() 函数
:
https://www.runoob.com/python/python-2x-3x.html
https://www.runoob.com/python/python-tutorial.html
https://www.runoob.com/python3/python3-tutorial.html
refs
https://www.cnblogs.com/xgqfrms/p/17269990.html#5162857
https://www.runoob.com/python/python-command-line-arguments.html
https://cloud.tencent.com/developer/article/1809689
https://www.cnblogs.com/mrwhite2020/p/16812198.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17273858.html
未经授权禁止转载,违者必究!