How to redirect both stdout and stderr to a file [duplicate]
If you want to log to the same file: command1 >> log_file 2>&1 If you want different files: command1 >> log_file 2>> err_file
If you want to log to the same file: command1 >> log_file 2>&1 If you want different files: command1 >> log_file 2>> err_file
Your code works when run in an script because Python encodes the output to whatever encoding your terminal application is using. If you are piping you must encode it yourself. A rule of thumb is: Always use Unicode internally. Decode what you receive, and encode what you send. # -*- coding: utf-8 -*- print u”åäö”.encode(‘utf-8’) … Read more
If you want to do the redirection within the Python script, setting sys.stdout to a file object does the trick: # for python3 import sys with open(‘file’, ‘w’) as sys.stdout: print(‘test’) A far more common method is to use shell redirection when executing (same on Windows and Linux): $ python3 foo.py > file
To expand on davor’s answer, you can use PowerShell like this: powershell “dir | tee test.txt” If you’re trying to redirect the output of an exe in the current directory, you need to use .\ on the filename, eg: powershell “.\something.exe | tee test.txt”
print is just a thin wrapper that formats the inputs (modifiable, but by default with a space between args and newline at the end) and calls the write function of a given object. By default this object is sys.stdout, but you can pass a file using the “chevron” form. For example: print >> open(‘file.txt’, ‘w’), … Read more
Just get a handle to the root logger and add the StreamHandler. The StreamHandler writes to stderr. Not sure if you really need stdout over stderr, but this is what I use when I setup the Python logger and I also add the FileHandler as well. Then all my logs go to both places (which … Read more
From Magnus Lycka answer on a mailing list: You can skip buffering for a whole python process using python -u (or #!/usr/bin/env python -u etc.) or by setting the environment variable PYTHONUNBUFFERED. You could also replace sys.stdout with some other stream like wrapper which does a flush after every call. class Unbuffered(object): def __init__(self, stream): … Read more
Take a look here. It should be: yourcommand &> filename It redirects both standard output and standard error to file filename.
First redirect stderr to stdout — the pipe; then redirect stdout to /dev/null (without changing where stderr is going): command 2>&1 >/dev/null | grep ‘something’ For the details of I/O redirection in all its variety, see the chapter on Redirections in the Bash reference manual. Note that the sequence of I/O redirections is interpreted left-to-right, … Read more
The command you want is named tee: foo | tee output.file For example, if you only care about stdout: ls -a | tee output.file If you want to include stderr, do: program [arguments…] 2>&1 | tee outfile 2>&1 redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout. … Read more