[issue38819] The redirect

Raymond Hettinger report at bugs.python.org
Sat Nov 16 00:52:38 EST 2019


Raymond Hettinger <raymond.hettinger at 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 <report at bugs.python.org>
<https://bugs.python.org/issue38819>
_______________________________________


More information about the Python-bugs-list mailing list