我有一个python脚本,它需要shell脚本中的值.以下是外壳程序脚本(a.sh):#!/bin/bashreturn_value(){value=$(///some unix command)echo $value}return_value以下是python脚本:Import subprocessanswer = Subp...

我有一个python脚本,它需要shell脚本中的值.
以下是外壳程序脚本(a.sh):
#!/bin/bash
return_value(){
value=$(///some unix command)
echo "$value"
}
return_value
以下是python脚本:
Import subprocess
answer = Subprocess.call([‘./a.sh’])
print("the answer is %s % answer")
但是它不起作用.错误是“ ImportError:没有名为子进程的模块”.我猜我的版本(Python 2.3.4)很老了.在这种情况下,是否可以替代子流程?
解决方法:
使用subprocess.check_output:
import subprocess
answer = subprocess.check_output(['./a.sh'])
print("the answer is {}".format(answer))
有关subprocess.check_output的帮助:
>>> print subprocess.check_output.__doc__
Run command with arguments and return its output as a byte string.
演示:
>>> import subprocess
>>> answer = subprocess.check_output(['./a.sh'])
>>> answer
'Hello World!\n'
>>> print("the answer is {}".format(answer))
the answer is Hello World!
a.sh:
#!/bin/bash
STR="Hello World!"
echo $STR
沃梦达教程
本文标题为:如何从python脚本中的shell脚本返回值


基础教程推荐
猜你喜欢
- python学习与数据挖掘应知应会的十大终端命令 2023-08-08
- 如何在Windows中的python中创建服务? 2023-11-10
- GraphLab-Create Module未安装在Python 3.5(ubuntu)上 2023-11-13
- Python网络活动统计信息Linux 2023-11-15
- 使用anaconda 3 for python 3.4在ubuntu 15.04上安装caffe-找不到模块caffe 2023-11-11
- 关于Python Tkinter 复选框 ->Checkbutton 2022-10-20
- Windows 下update pip 报错:Cannot remove entries from nonexistent file c:\intelpython3\lib\site-packa 2023-09-04
- 为什么python子进程输出与shell不同? 2023-11-14
- c-适用于Windows的python ncreduce 2023-11-12
- 如何在python中将二进制图像转换为内存中的数据结构? 2023-11-14