passing data to subprocess.check_output

python

Solution

Use `Popen.communicate` instead of `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")

Problem

I want to invoke a script, piping the contents of a string to its stdin and retrieving its stdout. I don't want to touch the real filesystem so I can't create real temporary files for it. using `subprocess.check_output` I can get whatever the script writes; how can I get the input string into its stdin though? ``` 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' ```

Original source