How to redirect print output to a text file in Python

Redirecting print output to a text file saves any printed data to the designated file.

Solution for How to redirect print output to a text file in Python : You can use sys.stdout to redirect print output to a text file Call open(file, mode) with mode as “w” to open a stream of a text file file for writing. Assign the result to sys.stdout. After printing one or more times, use file.close() with file as sys.stdout to save the redirected print output to the file.


how-to-redirect-print-output-to-a-text-file-in-python