【shell】 shell调用python脚本,并且向python脚本传递参数

您所在的位置:网站首页 python3脚本运行shell 【shell】 shell调用python脚本,并且向python脚本传递参数

【shell】 shell调用python脚本,并且向python脚本传递参数

2024-06-13 09:12| 来源: 网络整理| 查看: 265

文章目录 1.shell调用python脚本,并且向python脚本传递参数2. 调用python脚本中的某个方法

1.shell调用python脚本,并且向python脚本传递参数

shell中:

#定义变量 para1='' para2='' #调用py脚本,并传递参数 python test.py $para1 $para2

python文件test.py:

import sys #定义main,接收2个参数 def main(canshu1, canshu2) ..... #通过sys.argv获得入参 main(sys.argv[1], sys.argv[2]) 2. 调用python脚本中的某个方法

章节1中的是调用默认的main方法,如果想指定调用某个方法呢?

我们假设test.py中有个其他方法 test1和test2:

import sys #定义test1,无参数 def test() #定义test2,接收1个参数 def test2(param) .....

想通过shell调用里面的test,只需要在shell中执行一个调用的命令行即可:

python -c 'import test; print test.test1()'

如果想调用test2,需要传递一个参数:

param='abc' python -c "import test; print test.test2('${param}')"

注意外层引号为双引号,这样里面可以通过${}传递参数了,但是需要额外加单引号,表示字符串。

参考: 《shell调用python脚本,并且向python脚本传递参数》 《使用shell调用python中的函数》



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3