0

With python, I'm attempting to get the last line in the python console window. If I had a line in the console which had the example content "Hello, world!", how would I take that text and put it into a string, without changing stdout?

I've looked on Google and have found nothing that helps me. I found a way that works, but changes the stdout so I can't use print():

from io import StringIO
import sys

sys.stdout = buffer = StringIO()

print("Hello, world!")
with open("h.txt", "w") as h:
    h.write(buffer.getvalue())

In the normal Windows console, type h.txt shows "Hello, world!". This technically works, but redirects stdout. Plus, it doesn't get the last line exactly, and only gets the last thing written to stdout after it was redirected. I want to be able to get the last line of the console no matter when a variable was set, if that makes sense.

If this post needs clarification, I will try to answer any questions in the comments. Thank you!

Emily
  • 1
  • Do you mean you want to output something then get the last output? So you don't need to consider how to get text from stdout, you could just save the last line as a variable. Like: `last_line = "Hello World!"` – Yang HG Mar 01 '21 at 06:56
  • No, that's not what I meant. E.g.: If you run it from a CMD window, and the last text was `C:\>py main.py` or something, you would be able to get that. Also if someone was typing text into that bottom row the code could get whatever was there at that time. Does that make sense? – Emily Mar 01 '21 at 07:01
  • First explain what you want to do with this last line of the console – rioV8 Mar 01 '21 at 07:02
  • How are you able to get `C:\>py main.py` – rioV8 Mar 01 '21 at 07:03
  • If you want to get user input, just use `input()` is good enough. But if you want to get the line before your python scripts was running, I think it's impossible. – Yang HG Mar 01 '21 at 07:03
  • "First explain what you want to do with this last line of the console" Right now, I simply just want to put it into a variable. "How are you able to get `C:\>py main.py`" That is *literally* my entire question. "If you want to get user input, just use `input()` is good enough. But if you want to get the line before your python scripts was running, I think it's impossible" Yes, but this input is on the bottom line, so this should also be able to get that ideally. And fair I suppose. I hope it isn't impossible – Emily Mar 01 '21 at 07:05
  • Why do you need that line? – rioV8 Mar 01 '21 at 07:05
  • You say `you would be able to get that`, how do you get that – rioV8 Mar 01 '21 at 07:06
  • "Why do you need that line?" This doesn't relate to the topic though- and "you would be able to get that" was in a hypothetical. I meant that **when using the solution I'm looking for**, you would be able to get that line. – Emily Mar 01 '21 at 07:07
  • If you don't explain what you want to do with this line we can't suggest you an alternative, give examples of a line you need **before your python scripts was running** – rioV8 Mar 01 '21 at 07:10
  • I just did! The example was getting `C:\>py main.py` assuming that was what ran the python file – Emily Mar 01 '21 at 07:13
  • And i said what i wanted to do with the line, i said I wanted to put it in a variable, what i do with that variable doesn't matter – Emily Mar 01 '21 at 07:18
  • You wanted the line on the console **BEFORE** you ran your python file, that is the last line printed by the **PREVIOUS** command, your example shows the last line **YOUR OWN** script has printed, or do you want the command line of **YOUR OWN SCRIPT**. What do you want? – rioV8 Mar 01 '21 at 07:23
  • If you just want to get your script command line, `sys.argv` is good for you. – Yang HG Mar 01 '21 at 07:24
  • I just want to get the contents of the console, but preferably the last line of it – Emily Mar 01 '21 at 08:01
  • Then you have to find out the terminal process the current shell is running in and interrogate that process, if possible, and be aware that every type of terminal does it different, and your script could be running in several subshells – rioV8 Mar 01 '21 at 09:16

2 Answers2

0

Write a function myPrint that captures the last line printed

from io import StringIO

lastLine = ''

def myPrint(*args, **kwargs):
  buffer = StringIO()
  print(*args, **kwargs, file=buffer)
  lastLine = buffer.getvalue()
  print(lastLine, end='')

myPrint("Hello World!")

You still can use print if you rename the default function print

from io import StringIO

lastLine = ''

default_print = print

def print(*args, **kwargs):
  buffer = StringIO()
  default_print(*args, **kwargs, file=buffer)
  lastLine = buffer.getvalue()
  default_print(lastLine, end='')

print("Hello World!")
rioV8
  • 24,506
  • 3
  • 32
  • 49
0

Not entirely sure what you are trying to achieve with the code. As for the stdout getting redirected issue try the below code

from io import StringIO
import sys

sys.stdout = buffer = StringIO()

print("Hello, world!")
val = buffer.getvalue()
sys.stdout = sys.__stdout__

with open("h.txt", "w") as h:
    h.write(val)
    print(val)

Refer to this link Can I redirect the stdout in python into some sort of string buffer?

Ritwik G
  • 406
  • 2
  • 8