How to redirect print output to a file in Python

Redirecting print output to a file changes the destination of the output from the console to an opened file.

Solution for How to redirect print output to a file in Python : You can use print() to redirect output to a file Call open(file, mode) with mode as “w” to create a stream from file for writing. Use print(*objects, file) to print *objects to stream file. Call file.close() to close stream file.


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