Tanmay Bhatnagar
Tanmay Bhatnagar

Reputation: 2470

Capture the output of ONLY stdout in python

PYTHON VERSION - 3.5.2 OS - Ubuntu 16.04 LTS

I am currently using both stdout and print statements to write to the terminal. I want to capture the output of ONLY sys.stdout.write command and not the print commands So for eg if my code is -

import sys
sys.stdout.write("Hello")
print("PRINT")

I want to capture only "Hello" and not "PRINT".

I am currently using this :

x = subprocess.run(['python3 test.py'] , shell = True , stdout = subprocess.PIPE).stdout.decode('utf-8')

which gives me this output:

['HelloPRINT', '']

Upvotes: 1

Views: 3735

Answers (3)

user3657941
user3657941

Reputation:

You can define your own version of the print function like this:

import sys

def get_my_print(file=sys.stdout):
    _original_print = print
    def my_print(*objects, sep='', end='\n', file=file, flush=False):
        _original_print(*objects, sep='', end='\n', file=file, flush=False)
    return my_print

original_print = print
print("Redirect print to stderr...")
print = get_my_print(sys.stderr)

print("Print some numbers...")
for x in range(10):
    print(x)

print = original_print
print("Done.")

Console

$ python3 redirect_print.py 1> /dev/null
Print some numbers...
0
1
2
3
4
5
6
7
8
9

Upvotes: 1

furas
furas

Reputation: 142651

print() uses sys.stdout.write() to display text .

Besides suproccess see only text send by system and it doesn't know what you used to send text to system.

You can only in code redirect print() to sys.stderr

import sys
sys.stdout.write("Hello")
print("PRINT", file=sys.stderr)

and then you will still see all text when you run in console but you will get only sys.stdout when you use subprocess

You will also get only stdout in file if you use in console

python script.py > stdout.txt

or you redirect to other program

python script.py | sort

Instead of print(... ,file=sys.stderr) you can also use sys.stderr.write("PRINT\n") but you have to add "\n" at the end manually.

Upvotes: 3

Maximouse
Maximouse

Reputation: 4383

You can't do this because print() uses sys.stdout.write() internally. The following REPL session illustrates this:

>>> import sys
>>> class StdoutProxy:
...  def write(self, text):
...   return len(text) # do nothing
...
>>> sys.stdout = StdoutProxy()
>>> print("Hello!")
>>> sys.stdout = sys.__stdout__  # reset stdout to its original state
>>> print("Hello!")
Hello!

Upvotes: 1

Related Questions