mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
18 lines
401 B
Python
18 lines
401 B
Python
import sys
|
|
|
|
class RedirectStdoutTo:
|
|
def __init__(self, out_new):
|
|
self.out_new = out_new
|
|
|
|
def __enter__(self):
|
|
self.out_old = sys.stdout
|
|
sys.stdout = self.out_new
|
|
|
|
def __exit__(self, *args):
|
|
sys.stdout = self.out_old
|
|
|
|
print('A')
|
|
with open('out.log', mode='w', encoding='utf-8') as a_file, RedirectStdoutTo(a_file):
|
|
print('B')
|
|
print('C')
|