How to redirect standard output and errors to files
This is not the only post you will find on the internet about it, I create a personal post here more as a personal reference about this matter.
#Discarding output
If you just want to get rid of the output, you can redirect to void using:
When running a application in a non dev environment, you certainly want to log messages and errors to a file rather than just
displaying to the console.
Let’s assume we have a python script called foo.py
.
#Redirect standard output to the file app.log
Standard redirection is done with a single >. This will replace the file each time your restart your script. Two > will append to the file.
Note that you can simple empty a file with:
#Redirect error output to the file err.log
Our test python script foo.py
:
Error is accessible with 2
while standard output is 1
, so you can redirect error with:
#Redirect standard and error outputs to the same file all.log