如果我这样做:
import subprocess
from cStringIO import StringIO
subprocess.Popen(['grep','f'],stdout=subprocess.PIPE,stdin=StringIO('one\ntwo\nthree\nfour\nfive\nsix\n')).communicate()[0]
我得到:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py", line 533, in __init__
(p2cread, p2cwrite,
File "/build/toolchain/mac32/python-2.4.3/lib/python2.4/subprocess.py", line 830, in _get_handles
p2cread = stdin.fileno()
AttributeError: 'cStringIO.StringI' object has no attribute 'fileno'
显然是cStringIO。StringIO对象的嘎嘎声不够接近文件鸭子,不适合subprocess.Popen。我怎么解决这个问题呢?
Beware that Popen.communicate(input=s)may give you trouble ifsis too big, because apparently the parent process will buffer it before forking the child subprocess, meaning it needs "twice as much" used memory at that point (at least according to the "under the hood" explanation and linked documentation found here). In my particular case,swas a generator that was first fully expanded and only then written tostdin so the parent process was huge right before the child was spawned,
and no memory was left to fork it:
文件“/opt/local/stow/python-2.7.2/lib/python2.7/subprocess.py”,第1130行,在_execute_child中
自我。Pid = os.fork()
OSError: [Errno 12]不能分配内存
如果您使用的是Python 3.4或更高版本,那么有一个很好的解决方案。使用input参数而不是stdin参数,它接受bytes参数:
output_bytes = subprocess.check_output(
["sed", "s/foo/bar/"],
input=b"foo",
)
这适用于check_output和run,但不适用于call或check_call。
在Python 3.7+中,你还可以添加text=True,使check_output接受一个字符串作为输入并返回一个字符串(而不是字节):
output_string = subprocess.check_output(
["sed", "s/foo/bar/"],
input="foo",
text=True,
)