2012-04-11 101 views
26

我想调用脚本,管道字符串的内容,它的标准输入和检索它的标准输出。将数据传递到subprocess.check_output

我不想碰真实的文件系统,所以我不能为它创造真正的临时文件。

使用subprocess.check_output我能得到什么脚本编写;我怎样才能得到输入字符串到它的stdin?

subprocess.check_output([script_name,"-"],stdin="this is some input") 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python2.7/subprocess.py", line 537, in check_output 
    process = Popen(stdout=PIPE, *popenargs, **kwargs) 
    File "/usr/lib/python2.7/subprocess.py", line 672, in __init__ 
    errread, errwrite) = self._get_handles(stdin, stdout, stderr) 
    File "/usr/lib/python2.7/subprocess.py", line 1043, in _get_handles 
    p2cread = stdin.fileno() 
AttributeError: 'str' object has no attribute 'fileno' 
+2

'check_output()'的stdin' arg必须是文件对象,而不是字符串。 – jdi 2012-04-11 10:00:21

+0

@jdi显然;所以如何给它像文件一样嘎嘎叫,但不是文件? – Will 2012-04-11 10:06:58

+0

通过选择@larsmans答案:-)如果您不尝试过度使用check_output方便功能并仅执行正常的Popen +通信,则它更容易。否则,你需要手动打开自己的管道,然后将它传递给'check_output(stdin)',然后写入它。 – jdi 2012-04-11 10:09:02

回答

29

使用Popen.communicate,而不是subprocess.check_output

from subprocess import Popen, PIPE 

p = Popen([script_name, "-"], stdin=PIPE, stdout=PIPE, stderr=PIPE) 
stdout, stderr = p.communicate("this is some input") 
+0

我不得不删除“ - ”才能正常工作 – 2016-04-07 15:40:14

17

在Python 3.4和更高版本,可以使用输入关键字参数通过标准输入发送输入使用subprocess.check_output()

the standard library documentation for subprocess.check_output()报价时:

输入说法是传递给Popen.communicate()从而给 子的标准输入。如果使用它,则必须是字节序列或字符串,如果 universal_newlines=True。使用时,内部Popen对象是 自动创建与stdin=PIPEstdin参数可能不会 也可以使用。

例子:

>>> subprocess.check_output(["sed", "-e", "s/foo/bar/"], 
...       input=b"when in the course of fooman events\n") 
b'when in the course of barman events\n' 
>>> 
>>> # To send and receive strings instead of bytes, 
>>> # pass in universal_newlines=True 
>>> subprocess.check_output(["sed", "-e", "s/foo/bar/"], 
...       universal_newlines=True, 
...       input="when in the course of fooman events\n") 
'when in the course of barman events\n' 
4

下面是Python 2.7版与输入check_output反向移植版本。

from subprocess import (PIPE, Popen, CalledProcessError) 

def check_output_input(*popenargs, **kwargs): 
    """Run command with arguments and return its output as a byte string. 

    If the exit code was non-zero it raises a CalledProcessError. The 
    CalledProcessError object will have the return code in the returncode 
    attribute and output in the output attribute. 

    The arguments are the same as for the Popen constructor. Example: 

    >>> check_output(["ls", "-l", "/dev/null"]) 
    'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n' 

    The stdout argument is not allowed as it is used internally. 
    To capture standard error in the result, use stderr=STDOUT. 

    >>> check_output(["/bin/sh", "-c", 
    ...    "ls -l non_existent_file ; exit 0"], 
    ...    stderr=STDOUT) 
    'ls: non_existent_file: No such file or directory\n' 

    There is an additional optional argument, "input", allowing you to 
    pass a string to the subprocess's stdin. If you use this argument 
    you may not also use the Popen constructor's "stdin" argument, as 
    it too will be used internally. Example: 

    >>> check_output(["sed", "-e", "s/foo/bar/"], 
    ...    input=b"when in the course of fooman events\n") 
    b'when in the course of barman events\n' 

    If universal_newlines=True is passed, the return value will be a 
    string rather than bytes. 

    """ 
    if 'stdout' in kwargs: 
     raise ValueError('stdout argument not allowed, it will be overridden.') 
    if 'input' in kwargs: 
     if 'stdin' in kwargs: 
      raise ValueError('stdin and input arguments may not both be used.') 
     inputdata = kwargs['input'] 
     del kwargs['input'] 
     kwargs['stdin'] = PIPE 
    else: 
     inputdata = None 
    process = Popen(*popenargs, stdout=PIPE, **kwargs) 
    try: 
     output, unused_err = process.communicate(inputdata) 
    except: 
     process.kill() 
     process.wait() 
     raise 
    retcode = process.poll() 
    if retcode: 
     cmd = kwargs.get("args") 
     if cmd is None: 
      cmd = popenargs[0] 
     raise CalledProcessError(retcode, cmd, output=output) 
    return output