【问题标题】:buffering subprocess output to a file: io.UnsupportedOperation: fileno将子进程输出缓冲到文件:io.UnsupportedOperation:fileno
【发布时间】:2016-06-14 16:46:02
【问题描述】:

我想将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

有什么想法/建议吗?

系统一:CentOS6.7、Python3.3

系统2:MacOSX10.10.5、Python3.5

【问题讨论】:

  • "-n1"后面应该有逗号吗?
  • 语法上没关系

标签: python io subprocess


【解决方案1】:

您可以通过让子进程为您将标准输出读入字节字符串来做您想做的事情。用

替换你的第二个电话
p1 = subprocess.Popen(["head", "-n5" ], stdin=p0.stdout, stdout=subprocess.PIPE)
data = p1.communicate()[0]
string = data.decode('utf-8')

【讨论】:

  • 这是一个好的开始,但你如何在pandas 中提供它?它不会将字节作为read_table 的输入。
  • 如果您知道原始文件使用什么编码,则可以将字节转换为字符串。比如添加string = data.decode('utf-8'),那么你就有了一个python字符串。
猜你喜欢
  • 2011-08-20
  • 2020-08-18
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-06
  • 2012-10-22
相关资源
最近更新 更多