How can I get the output of a process run using
StringIO.StringIO object to
stdout gives this error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 444, in call return Popen(*popenargs, **kwargs).wait() File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 588, in __init__ errread, errwrite) = self._get_handles(stdin, stdout, stderr) File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 945, in _get_handles c2pwrite = stdout.fileno() AttributeError: StringIO instance has no attribute "fileno" >>>
If you have Python version >= 2.7, you can use subprocess.check_output which basically does exactly what you want (it returns standard output as string).
Simple example (linux version, see note):
import subprocess print subprocess.check_output(["ping", "-c", "1", "220.127.116.11"])
Note that the ping command is using linux notation (
-c for count). If you try this on Windows remember to change it to
-n for same result.
As commented below you can find a more detailed explanation in this other answer.
subprocess.call() should only be redirected to files.
You should use
subprocess.Popen() instead. Then you can pass
subprocess.PIPE for the stderr, stdout, and/or stdin parameters and read from the pipes by using the
from subprocess import Popen, PIPE p = Popen(["program", "arg1"], stdin=PIPE, stdout=PIPE, stderr=PIPE) output, err = p.communicate(b"input data that is passed to subprocess" stdin") rc = p.returncode
The reasoning is that the file-like object used by
subprocess.call() must have a real file descriptor, and thus implement the
fileno() method. Just using any file-like object won"t do the trick.
See here for more info.
For python 3.5+ it is recommended that you use the run function from the subprocess module. This returns a
CompletedProcess object, from which you can easily obtain the output as well as return code.
from subprocess import PIPE, run command = ["echo", "hello"] result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True) print(result.returncode, result.stdout, result.stderr)
Cloud computing provides the capability to use computing and storage resources on a metered basis and reduce the investments in an organization’s computing infrastructure. The spawning and deletion ...
We are witnessing a movement that will completely transform any part of business and society. The word we have given to this move- ment is Big Data and it will change everything, from the way banks an...
Executing Data Quality Projects: Ten Steps to Quality Data and Trusted Information (TM), 2nd Edition. Execute Data Quality Projects, Second Edition presents a structured yet flexible approach to cr...
Systems programming provides the basis for global calculation. Developing performance-sensitive code requires a programming language that allows programmers to control the use of memory, processor tim...