xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Python 脚本接收命令行参数的多种方式 All In One

Python 脚本接收命令行参数的多种方式 All In One

  1. sys

  2. argparse

  3. shell script

  4. tensorflow

  5. 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('结束闪烁 👌🏻')


image

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-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(444)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-03-30 React Animation All In One
2022-03-30 TypeScript namespace All In One
2022-03-30 macOS Monterey 12.3 All In One
2021-03-30 uni-app & nvue & cli
2021-03-30 如何使用 Apple Watch 上 NFC 功能复制门禁卡 All In One
2020-03-30 @bind decorator
2020-03-30 TypeScript & global.d.ts All In One
点击右上角即可分享
微信分享提示