from io import StringIO
import sys
class OutputInterceptor(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio
sys.stdout = self._stdout
with OutputInterceptor() as output:
# Любой вывод в консоль из этого блока будет сохраняться в переменную output
print('123')
print('\n'.join(output))
from os import system
buffer = []
system('cls')
buffer.append('Hello')
print('\n'.join(buffer))
system('cls')
buffer.append('My number is 1')
print('\n'.join(buffer))
system('cls')
buffer.append('My number is 2')
print('\n'.join(buffer))