-2

Is there a way to find the previous character(s) in the console in python?

print('Hello', end='')

should return 'o'.

If there is a way to do this, how can I get the previous x characters? e.g. 'lo' if I want 2 characters. Should also work for tracebacks.

weasel
  • 534
  • 1
  • 5
  • 18
  • 2
    There's no clean way to get back data you sent to the stdout stream. However, you can redirect stdout to string that you can inspect later: https://stackoverflow.com/questions/1218933/can-i-redirect-the-stdout-in-python-into-some-sort-of-string-buffer – Mike67 Aug 03 '20 at 02:56
  • 2
    Standard output is an _output_, not an input. Unless you're just trying to do some logging or record a run of something, that content is too late to work with at that point. If you wanted to work with `'Hello'`, you should save it to a variable with e.g. `my_string = 'Hello'`. If you want to work with the text of tracebacks, you can catch the exception and save it to a variable. – TigerhawkT3 Aug 03 '20 at 03:01

2 Answers2

0

Have you tried using subprocess and PIPE your output of the script you are running. Ex: You want to get console output from:

#script1.py
print('Hello',end='')

Creating a subprocess will let you run and get the STDOUT & STDERR

#subproc.py
from subprocess import Popen, PIPE
process = Popen(['python','script1.py'],stdout=PIPE,stderr=PIPE,shell=True)
stdout,stderr = process.communicate()
print(stdout.decode('utf-8')) #console output
print(stderr.decode('utf-8')) #traceback, empty if there's no error

#From there you can manipulate string
print(stdout.decode('utf-8')[-1:]) #will print string 'o'
Ritsard
  • 51
  • 1
  • 7
0

Perhaps you need something similar that what Expect does.

Check this code:

import pty
import os

my_list_accumulator = []

def read_stuff(fd):
    output = os.read(fd, 1024)
    my_list_accumulator.append(output)
    return output

pty.spawn('python3', read_stuff)

Demo:

>>> import pty
>>> import os
>>> 
>>> my_list_accumulator = []
>>> 
>>> def read_stuff(fd):
...     output = os.read(fd, 1024)
...     my_list_accumulator.append(output)
...     return output
...     
... 
>>> pty.spawn('python3', read_stuff)
Python 3.5.2 (default, Jul 17 2020, 14:04:10) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('Hello', end='')
Hello>>> And this will give error
  File "<stdin>", line 1
    And this will give error
           ^
SyntaxError: invalid syntax
>>> 
0
>>> # pressed Ctrl-D to escape
... 
>>> print(b''.join(my_list_accumulator).decode('utf-8'))
Python 3.5.2 (default, Jul 17 2020, 14:04:10) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('Hello', end='')
Hello>>> And this will give error
  File "<stdin>", line 1
    And this will give error
           ^
SyntaxError: invalid syntax
>>> 

>>> 

So, you can modify the read_stuff function to do what you wish, but make sure to read the documentation: https://docs.python.org/3/library/pty.html

user
  • 675
  • 4
  • 11