import os import sys if __name__ == \'__main__\': out_fd = os.open(\'out.txt\', os.O_CREAT | os.O_WRONLY) os.dup2(out_fd, sys.stdout