Raymond Hettinger <raymond.hettin...@gmail.com> added the comment:
If your goal is to temporarily redirect stdout, there are a couple ways to do it: with open('dest1.txt', 'w') as target_file: print('hello world', file=target_file) or: with open('dest1.txt', 'w') as target_file: with contextlib.redirect_stdout(target_file) print('hello world') Both of these techniques temporarily alter where printing is directed. In contrast, the code posted above permanently alters the target unless explictly reset. ---------- nosy: +rhettinger _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue38819> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com