[PYTHON] Decorator to silence standard output

It is not necessary to use logging, but a little debug comment A decorator that temporarily devnulls stdout when you want to silence

debug.py


def mute(func):
    def _f(*args, **kwargs):
        sys.stdout = open(os.devnull, 'w')
        res = func(*args, **kwargs)
        sys.stdout.close()
        sys.stdout = sys.__stdout__
        return res
    return _f

Reference: Easily cProfile with a decorator http://qiita.com/mojaie/items/e14c3db9f8fdec896f8a

Recommended Posts

Decorator to silence standard output
Write standard output to a file
Decorator to retry
How to output "Ketsumaimo" as standard output in Python
[CentOS8] How to output Python standard output to systemd log
python decorator to retry
Theme: Standard output [Difficulty ★★★★★]
Limited standard output problem
Standard input / output summary
Change the standard output destination to a file in Python
Test standard output with Pytest
Output to syslog with Loguru
How to use the decorator
Convert to a string while outputting standard output with Python subprocess
When you want to print to standard output with print while testing with pytest
Post Jenkins console output to Slack
Export Google Analytics Standard to BigQuery
Output to csv file with Python
Output from Raspberry Pi to Line
Make standard output non-blocking in Python
Output cell to file with Colaboratory
[Django] Command to output QuerySet to csv
How to overwrite the output to the console