Is there a way to get whats currently displayed on the windows command prompt?
For example:
print('some text')
print('some different text')
>>> some text
>>> some different text
a = save_whats_on_cmd()
>>> a = 'some text \n some different text'
CodePudding user response:
Create a file, let say mylib.py
import sys
class Logger(object):
def __init__(self):
self.terminal = sys.stdout
self.log = open('log.dat', 'w')
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
Then open your interpreter:
>>> from mylib import Logger
>>> import sys
>>> sys.stdout = Logger()
>>> print(1)
1
>>> print(2)
2
>>> print(3)
3
>>> exit()
See log.dat:
1
2
3
CodePudding user response:
import sys
sys.stdout = open("test.txt", "w")
print("Hello World")
sys.stdout.close()
test.txt will have Hello World