1

使用python脚本传递参数:(三种方式可收藏) - Mrwhite86

 1 year ago
source link: https://www.cnblogs.com/mrwhite2020/p/16812198.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用python脚本传递参数:(三种方式可收藏)

背景:使用python脚本传递参数在实际工作过程中还是比较常用,以下提供了好几种的实现方式:

一、使用sys.argv的数组传入
说明:使用sys.argv必须按照先后的顺序传入对应的参数;sys.argv则封装了传入的参数数据,作为数组的方式已经传入

import sys

print("传入参数的总长度为:", len(sys.argv))
print("type:", type(sys.argv))
print("function name:", sys.argv[0])
try:
    print("第一个传入的参数为:", sys.argv[1])
    print("第二个传入的参数为:", sys.argv[2])
except Exception as e:
    print("Input Error:", e)

通过命令行查看运行结果:

1767782-20221204235602387-1078667055.png

二、使用argparse包传入

说明:parser.add_argument 方法的type参数理论上可以是任何合法的类型,而且传入的顺序并没有要求

参数使用的说明:

--xx  完整参数输入
-x    简写参数输入
type  输入的参数将要被转换的数据类型
default 默认值
help   参数介绍
nargs  可传入的参数数量
required  是否为必须参数
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)

查看运行结果:

1.使用完整参数输入

1767782-20221205001208762-80714195.png

2.使用简写参数输入

1767782-20221205001430643-1276786882.png

3.多参数的传入

1767782-20221205001827949-397119329.png

三、使用shell脚本的方式向python脚本传递参数

1.使用shell脚本向python脚本传递参数

python脚本依然使用sys.argv的数组的方式传入参数

原python如下:(python_test.py)

import sys

print("传入参数的总长度为:", len(sys.argv))
print("type:", type(sys.argv))
print("function name:", sys.argv[0])
try:
    print("第一个传入的参数为:", sys.argv[1])
    print("第二个传入的参数为:", sys.argv[2])
except Exception as e:
    print("Input Error:", e)

增加shell脚本如下:(shell_test.sh)

#!/bin/bash
#定义变量
para1=$1
para2=$2
python python_test.py $para1 $para2

查看运行结果:

1767782-20221205115628620-518169012.png

2.使用shell脚本向python脚本内的方法传递参数

python脚本如下:(python_test.py)

def fun1():
    return "无参数方法fun1"

def fun2(x):
    return f"有参数方法fun2且传入的参数为{x}"

shell脚本如下:(shell_test.sh)

(1)无参数方式调用:

#!/bin/bash
#定义变量
python -c 'import python_test;print(python_test.fun1())'

查看运行结果:

1767782-20221205155132527-1720748656.png

 (2)有参数方式调用

#!/bin/bash
#定义变量
para=$1
python -c "import python_test;print(python_test.fun2('${para}'))"

查看运行结果:

1767782-20221205161019118-1289830887.png

 (3)一次调用所有方法,放入一个集合中,再调用切割方法获取相应的值

#!/bin/bash
#定义变量
para=$1
#将结果保存为一个变量ALL_RESULTS内
ALL_RESULTS=$(python -c "import python_test;print(python_test.fun1(),python_test.fun2('${para}'))")
#分别打印第一个方法的返回与第二个方法的返回
RESULTS_fun1=$(echo $ALL_RESULTS | cut -d' ' -f1)
RESULTS_fun2=$(echo $ALL_RESULTS | cut -d' ' -f2)
echo fun1方法的返回结果为:$RESULTS_fun1
echo fun2方法的返回结果为:$RESULTS_fun2

查看运行结果:

1767782-20221205161633168-1854362355.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK