缓冲子进程输出到文件:io.UnsupportedOperation:fileno

时间:2016-06-14 16:46:02

标签: python io subprocess

我想将grep的输出缓冲到缓冲区,然后用pandas读取它,以避免将大量原始文件加载到内存中:

import subprocess
import io
import pandas as pd

firstfile = "~/references/rs_hg19.snps.uniq.bed"
outf = io.StringIO("")
p0 = subprocess.Popen(('grep', '-P', "chr22\\t", firstfile), stdout=subprocess.PIPE)
p1 = subprocess.call(["head", "-n5", ], stdin=p0.stdout, stdout= outf)
p0.wait()
print(pd.read_table(outf)
    )

我收到错误:

Traceback (most recent call last)
  File "test.py", line 9, in <module>
    p1 = subprocess.call(["head", "-n1", ], stdin=p0.stdout, stdout= outf)
  File "/opt/rh/python33/root/usr/lib64/python3.3/subprocess.py", line 520, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/opt/rh/python33/root/usr/lib64/python3.3/subprocess.py", line 786, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "/opt/rh/python33/root/usr/lib64/python3.3/subprocess.py", line 1294, in _get_handles
    c2pwrite = stdout.fileno()
io.UnsupportedOperation: fileno

即使我直接运行not from any IDE

有任何想法/建议吗?

系统1:CentOS6.7,Python3.3

系统2:MacOSX10.10.5,Python3.5

1 个答案:

答案 0 :(得分:1)

您可以通过让子进程将stdout读取为字节字符串来执行您想要的操作。用

替换你的第二个电话
Circle