(Apr-25-2017, 06:16 AM)Skaperen Wrote: i'm assuming you meant for stdout_bak and output_back to be the same.
Right! I fixed it.
import sys
stdout_bak = sys.stdout
with open('output.txt', 'w') as sys.stdout:
print("I've never did this before so now I'm experimenting")
print("After that output.txt should containd this prints")
sys.stdout = stdout_bak # you must return everything as it was before to print normally again
print('After the first with statement')
with open('output.txt', 'r') as in_file:
print(in_file.read())
Output:
victor@jerry:~$ python3 /tmp/test.py
After the first with statement
I've never did this before so now I'm experimenting
After that output.txt should containd this prints