【问题标题】:Capture value from shell script and use it in Python without using subprocess从 shell 脚本中捕获值并在 Python 中使用它而不使用子进程
【发布时间】:2013-06-18 22:04:34
【问题描述】:

我有一个 python 脚本,它需要一个来自 shell 脚本的值。

以下是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 : No module named subprocess

我猜我的版本(Python 2.3.4)已经很老了。在这种情况下是否可以应用subprocess 的替代品??

【问题讨论】:

    标签: python linux bash subprocess python-2.3


    【解决方案1】:

    尝试 py2.3.​​4 的 commands 模块,注意该模块自 py2.6 起已被弃用:

    使用commands.getoutput:

    import commands
    answer = commands.getoutput('./a.sh')
    

    【讨论】:

    • 成功了!!!!你怎么看这个?我知道我们的团队需要更新我们的 python 版本,但非常感谢!
    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 2022-06-12
    • 1970-01-01
    • 2012-04-22
    相关资源
    最近更新 更多