Hello,
I wrote a class that toggles stdio from within a script.
Be aware that it
see the __name__ clause for usage
save the following as 'ToggleStdio.py'
then use as in __name__
Larz60+
I wrote a class that toggles stdio from within a script.
Be aware that it
see the __name__ clause for usage
save the following as 'ToggleStdio.py'
# add import sys class ToggleStdio: def __init__(self, filename=None): self.savestdout = None self.stdio_redirected = False if filename: self.savefile = filename def toggle_stdout(self): if self.stdio_redirected: sys.stdout = self.savestdout self.savestdout = None self.stdio_redirected = False else: self.savestdout = sys.stdout sys.stdout = open(self.savefile, 'w') self.stdio_redirected = True if __name__ == '__main__': ts = ToggleStdio('mystdio.txt') ts.toggle_stdout() print('This should go to file') ts.toggle_stdout() print('This should go to screen') ts.toggle_stdout() print('added to file') print('more added to file') ts.toggle_stdout() print('more on screen')To use from your class, import ToggleStdio
then use as in __name__
Larz60+