【发布时间】:2015-03-27 15:54:08
【问题描述】:
为什么这段代码在终端(Ubuntu 12.04)运行成功:
python -c "print 123" > >(tee stdout.txt)
但是python中的这段代码(2.7.5):
import os
os.system('python -c "print 123" > >(tee stdout.txt)') # same command
产生这个错误?
sh: -c: line 0: syntax error near unexpected token `>'
sh: -c: line 0: `python -c "print 123" > >(tee stdout.txt)'
【问题讨论】:
-
需要传递参数
shell=True。更好的是,使用本机 python。出于兴趣,您为什么使用> >(tee ...)而不仅仅是| tee? -
感谢原生 python 建议。无论如何,我想用任何命令解决这个问题(例如
echo hello)。如果我尝试传递参数shell=True我得到TypeError: system() takes no keyword arguments -
我正在尝试使用以下命令运行命令:stackoverflow.com/a/692407/1112654 这个问题是问题的足够简化版本。
-
@axelbrz:使用
subprocess.call,您可以从执行的命令中(分别)接收stdout 和stderr,然后在Python 中对它们做任何您想做的事情。最终结果可能会更好。
标签: python bash shell unix ubuntu