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!