python-progressbar icon indicating copy to clipboard operation
python-progressbar copied to clipboard

Is there an API to save the print of the progressbar to the log.txt file?

Open prstrive opened this issue 3 years ago • 1 comments
trafficstars

Description

I want to save the print of the Progressbar to a log.txt file after pbar.finish(). But it seems that the print of the Progressbar cannot be directly output to the log.txt file like ordinary stdout. So I would like to ask if there are any related APIs or some suggestions?

prstrive avatar Dec 09 '21 02:12 prstrive

By default the progressbar writes to stderr, you can change this behaviour by specifying a fd parameter: https://progressbar-2.readthedocs.io/en/latest/progressbar.bar.html#progressbar.bar.DefaultFdMixin It defaults to sys.stderr but you can also pass open('logfile.txt')

If you're executing the script on a linux/unix/mac system you can also redirect the output using shell redirects:

# python3 your_script.py 2>&1 > logfile.txt

stream 2 is stderr, 1 is stdout, so we're redirecting stderr to the stdout stream.

wolph avatar Dec 09 '21 19:12 wolph