Showing posts with label redirect. Show all posts
Showing posts with label redirect. Show all posts

24 March 2021

How to redirect 'print' output to a file using python?

 How to redirect 'print' output to a file using python?




import sys

orig_stdout = sys.stdout
f = open('out.txt', 'w')
sys.stdout = f

for i in range(2):
    print 'i = ', i

sys.stdout = orig_stdout
f.close()