Python redirect stdout/output to file

This is really really neat!

def redirect_to_file(text):
    original = sys.stdout
    sys.stdout = open('/path/to/redirect.txt', 'w')
    print('This is your redirected text:')
    print(text)
    sys.stdout = original
 
    print('This string goes to stdout, NOT the file!')

(from Python 101: Redirecting stdout - The Mouse Vs. The Python)