Skip to main content
 首页 » 编程设计

python之如何从sh写入文件

2025年05月04日17tintown

我有一个脚本,我喜欢通过 subprocess 在 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 中运行的步骤并解释原因吗。

请您参考如下方法:

您必须使用 -c 参数运行 sh:

subprocess.check_call( ['sh', '-c', command] )