Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture Python interpreter's and/or CMD.EXE's output from a Python script?

  1. Is it possible to capture Python interpreter's output from a Python script?
  2. Is it possible to capture Windows CMD's output from a Python script?

If so, which librar(y|ies) should I look into?

like image 206
guillermooo Avatar asked Aug 24 '08 08:08

guillermooo


People also ask

How do you capture standard output in Python?

When you use print() in python the output goes to standard output or sys. stdout . You can directly call sys. stdout.

How do you record stdout output from a Python function call?

To capture stdout output from a Python function call, we can use the redirect_stdout function. to call redirect_stdout with the f StringIO object. Then we call do_something which prints stuff to stdout. And then we get the value printed to stdout with f.

How do I run Python interpreter from command line?

Windows: type "powershell" in the lower left, this opens the Windows command line terminal. In the terminal type the command "python3" ("python" on Windows, or sometimes "py"). This runs the interpreter program directly. On the Mac type ctrl-d to exit (on Windows ctrl-z).

How do I run a .py file in CMD?

Using the python Command To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


2 Answers

If you are talking about the python interpreter or CMD.exe that is the 'parent' of your script then no, it isn't possible. In every POSIX-like system (now you're running Windows, it seems, and that might have some quirk I don't know about, YMMV) each process has three streams, standard input, standard output and standard error. Bu default (when running in a console) these are directed to the console, but redirection is possible using the pipe notation:

python script_a.py | python script_b.py

This ties the standard output stream of script a to the standard input stream of script B. Standard error still goes to the console in this example. See the article on standard streams on Wikipedia.

If you're talking about a child process, you can launch it from python like so (stdin is also an option if you want two way communication):

import subprocess
# Of course you can open things other than python here :)
process = subprocess.Popen(["python", "main.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
x = process.stderr.readline()
y = process.stdout.readline()
process.wait()

See the Python subprocess module for information on managing the process. For communication, the process.stdin and process.stdout pipes are considered standard file objects.

For use with pipes, reading from standard input as lassevk suggested you'd do something like this:

import sys
x = sys.stderr.readline()
y = sys.stdin.readline()

sys.stdin and sys.stdout are standard file objects as noted above, defined in the sys module. You might also want to take a look at the pipes module.

Reading data with readline() as in my example is a pretty naïve way of getting data though. If the output is not line-oriented or indeterministic you probably want to look into polling which unfortunately does not work in windows, but I'm sure there's some alternative out there.

like image 106
Henrik Gustafsson Avatar answered Sep 18 '22 22:09

Henrik Gustafsson


I think I can point you to a good answer for the first part of your question.

1.  Is it possible to capture Python interpreter's output from a Python script?

The answer is "yes", and personally I like the following lifted from the examples in the PEP 343 -- The "with" Statement document.

from contextlib import contextmanager
import sys

@contextmanager
def stdout_redirected(new_stdout):
    saved_stdout = sys.stdout
    sys.stdout = new_stdout
    try:
        yield None
    finally:
        sys.stdout.close()
        sys.stdout = saved_stdout

And used like this:

with stdout_redirected(open("filename.txt", "w")):
    print "Hello world"

A nice aspect of it is that it can be applied selectively around just a portion of a script's execution, rather than its entire extent, and stays in effect even when unhandled exceptions are raised within its context. If you re-open the file in append-mode after its first use, you can accumulate the results into a single file:

with stdout_redirected(open("filename.txt", "w")):
    print "Hello world"

print "screen only output again"

with stdout_redirected(open("filename.txt", "a")):
    print "Hello world2"

Of course, the above could also be extended to also redirect sys.stderr to the same or another file. Also see this answer to a related question.

like image 43
martineau Avatar answered Sep 19 '22 22:09

martineau