【发布时间】:2016-06-26 06:47:52
【问题描述】:
我正在尝试从 python 运行 shell 命令,但是一旦添加管道,我就会收到“OSError: [Errno 2] No such file or directory”错误。我尝试了各种方法,甚至直接在 /bin/grep 中引用了 grep。知道我做错了什么吗?
作品:
import subprocess
p = subprocess.Popen(["ls"], stdout=subprocess.PIPE)
out, err = p.communicate()
print out
不起作用:
import subprocess
p = subprocess.Popen(["ls | grep '20'"], stdout=subprocess.PIPE)
out, err = p.communicate()
print out
显示错误:
[OMITTED]$ python test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
p = subprocess.Popen(["ls | grep '20'"], stdout=subprocess.PIPE)
File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
[OMITTED]$
版本: Python 2.6.6(r266:84292,2014 年 1 月 22 日,09:42:36) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] 在 linux2 上
【问题讨论】:
标签: python