Python library to tee stderr / stdout to a file
Installation
pip install tee
Quick Start
tee_test.py
import sys
from tee import StdoutTee, StderrTee
with StdoutTee("mystdout.txt"), StderrTee("mystderr.txt"):
sys.stdout.write("[stdout] hello\n")
sys.stderr.write("[stderr] hello\n")
sys.stdout.write("[stdout] world\n")
sys.stderr.write("[stderr] world\n")
sys.stdout.write("[stdout] not going to be written to file\n")
sys.stderr.write("[stderr] not going to be written to file\n")
$ python tee_test.py
[stdout] hello
[stderr] hello
[stdout] world
[stderr] world
[stdout] not going to be written to file
[stderr] not going to be written to file
$ cat mystdout.txt
[stdout] hello
[stdout] world
$ cat mystderr.txt
[stderr] hello
[stderr] world
Filters
StdoutTee and StderrTee take filters as parameters which run before writing to a file or the stream. These filters are callables that take the message to be written as input and return either None or a new message.
I find them particularly useful when you want to write colorized output to the stream, but strip out the control characters when writing to a file, especially when using fabric.
import re
import tee
from fabric.api import run
def _remove_control_chars(message):
return re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]', "", message)
def echo_color():
with tee.StdoutTee("fabout.txt", mode="a", file_filters=[_remove_control_chars]):
run("""echo -e "\E[1;32mHello World \E[4;31mLets add some\E[0m\E[1;34m color" && tput sgr0""")
fab -H localhost echo_color
Metadata
Release files for tee 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tee-0.0.3.tar.gz | 3.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tee-0.0.3-py2-none-any.whl | Python 2 | none | any | Details |
Total release size: 6.2 kB
Release files / tee-0.0.3.tar.gz
| Download URL | tee-0.0.3.tar.gz |
|---|---|
| Size | 3.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
801dea18fe5046b57c26e87aa5c7ca0d1b8590cdc0e4e84158d07cdfa6628cdb
|
|
BLAKE2b-256 checksum How to use checksums |
44134daa2418b6c72de8255da00f873d709317accd57f38c6d8bf0233aeb9bf2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / tee-0.0.3-py2-none-any.whl
| Download URL | tee-0.0.3-py2-none-any.whl |
|---|---|
| Size | 3.1 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
16fdbb34b1e89a308094d86d2d497f5fa5ac47df9202f3dde913070d31699fad
|
|
BLAKE2b-256 checksum How to use checksums |
ea8d3505b05e91bc2099fac84365bcd4581b7099c4c456051fed6b6377a751ba
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |