Skip to content Skip to sidebar Skip to footer

How To Write Console Output On Text File

I am new to programming and I've searched the webpage for the answer to this question and have tried many possibilities without success. I have currently managed to connect a poten

Solution 1:

When you execute a file in the terminal, you can redirect the outputs of this script to a file like this:

$ python script.py > /the/path/to/your/file

In Python you just have to set the sys.stdout to a file and then, all the prints will be redirected to /the/path/to/your/file.

import sys
sys.stdout = open('/the/path/to/your/file', 'w')

and do not forget to close the file at the end of your script ;)

sys.stdout.close()

Post a Comment for "How To Write Console Output On Text File"