我有一个脚本,喜欢通过子进程在python中执行(是的,它必须在sh中)。
现在,我这样调用sh

subprocess.check_call( ['sh' + command] )


其中command是:

echo 'someformat : : '${ENV_VAR}'/efc ;' > targetfile


可悲的是这给了我:

sh: 0: Can't open echo 'someformat : : '${ENV_VAR}'/efc ;' > targetfile


有人可以指导我逐步完成使该命令在sh中运行的原因,并解释原因。

最佳答案

尝试这个:

command = "echo 'someformat : : '${ENV_VAR}'/efc ;' > targetfile"
subprocess.check_call(["sh", "-c", command])


参数-c修改sh行为以从下一个参数的字符串读取命令。
并且参数必须包含在列表中。


python 2 subprocess doc
python 3 subprocess doc

关于python - 如何从sh写入文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16230603/

10-09 06:14
查看更多