0

I´m trying to get last python output and I dont know how to do it.

Basically what Iím trying to do is detect the last output so, for example:

print("Hello World")
last_output = get_last_output()
print() # For avoiding confutions

print(last_output)
# Would print "\n"


print("Hello World", end="")
last_output = get_last_output()
print() # For avoiding confutions

print(last_output)
# Would print "Hello World"

I would also love this awnser would work independently of the console

Baelfire18
  • 175
  • 2
  • 7
  • Do you want `subprocess.check_output`? It's basically `Popen` but it's sync and blocks your code until it finishes. – Eric Jin Jun 10 '22 at 14:32
  • It "does" or "doesn't" seem to work? If it "doesn't", what exactly doesn't work? – Yevhen Kuzmovych Jun 10 '22 at 14:32
  • @EricJin how would you use it in this case? I did not understand its documentation https://docs.python.org/3/library/subprocess.html#subprocess.check_output – Baelfire18 Jun 10 '22 at 14:45
  • @YevhenKuzmovych they do not print the latest output in the file, they just print in bytes the current's file path – Baelfire18 Jun 10 '22 at 14:52
  • I see now, so you want to capture the output to stdout of the last function that was run? You're going to need to read from `sys.stdout`. – Eric Jin Jun 10 '22 at 15:10
  • Yes, how can I use `sys.stdout` to read always from the last line of the output? – Baelfire18 Jun 10 '22 at 15:15
  • You would probably need to seek backwards until you hit a newline. Or use `readlines` and take the last one. – Eric Jin Jun 10 '22 at 16:42
  • I have tried and can seem to make it work, can you show me how would you do it? @EricJin – Baelfire18 Jun 10 '22 at 18:46
  • Nevermind, you're not able to read from stdout. But you can execute whole python files with it and check the output. What is your objective anyway? – Eric Jin Jun 10 '22 at 20:51

1 Answers1

2

Assuming "the last output" is the last not empty string written to sys.stdout, one option is to assign an object with the write(data) and flush() methods to sys.stdout, so you can save what should be the output:

import sys


class StdoutHandler:
    def __init__(self):
        self.last_output = ""
    
    def start(self):
        self._handled_stdout = sys.stdout
        sys.stdout = self
    
    def write(self, data: str):
        # write(data="") is called for the end kwarg in print(..., end="")
        if data:
            self.last_output = data
            self._handled_stdout.write(data)

    def end(self):
        sys.stdout = self._handled_stdout
    
    def flush(self):
        self._handled_stdout.flush()



stdout_handler = StdoutHandler()
stdout_handler.start()


print("Hello World")
last_output = stdout_handler.last_output
print(repr(last_output))
# Prints '\n'


print("Hello World", end="")
last_output = stdout_handler.last_output
print()
print(repr(last_output))
# Prints 'Hello World'

print("Hello", "World", end="")
last_output = stdout_handler.last_output
print()
print(repr(last_output))
# Prints 'World'

I got the idea from How to duplicate sys.stdout to a log file?

Disclaimer: @Baelfire18 asked me to help him answer this question

Benjamín V
  • 116
  • 2
  • 6