0

I'm redirecting the stdout and stderr streams from a Python script to a file. Before redirecting, I was using the tqdm progress bar in my script which continually updates the bar in the same place in the console. However, after redirecting, in the file each update is printed to a new line:

  0%|          | 0/1369 [00:00<?, ?it/s]
  0%|          | 1/1369 [00:00<08:40,  2.63it/s]
  0%|          | 2/1369 [00:00<08:03,  2.82it/s]
  0%|          | 3/1369 [00:00<07:35,  3.00it/s]
  0%|          | 4/1369 [00:01<07:15,  3.14it/s]
  0%|          | 5/1369 [00:01<07:52,  2.89it/s]
  0%|          | 6/1369 [00:01<07:26,  3.05it/s]
  1%|          | 7/1369 [00:02<07:08,  3.18it/s]
  1%|          | 8/1369 [00:02<06:55,  3.28it/s]
  1%|          | 9/1369 [00:02<06:46,  3.35it/s]
  1%|          | 10/1369 [00:03<06:39,  3.40it/s]
  1%|          | 11/1369 [00:03<06:35,  3.43it/s]
  1%|          | 12/1369 [00:03<06:32,  3.46it/s]
  1%|          | 13/1369 [00:03<06:29,  3.48it/s]
  1%|          | 14/1369 [00:04<06:28,  3.49it/s]

Is there a way to write tqdm output (or any other progress bar) to a file which stays on the same line?

user58338
  • 79
  • 6
  • Not an exact duplicate but this might help: https://stackoverflow.com/questions/36986929/redirect-print-command-in-python-script-through-tqdm-write – Selcuk Jan 18 '21 at 01:06
  • Wouldn't it be better to use `tqdm(..., disable=True)` for redirection? – ghchoi Jan 18 '21 at 01:13
  • @Selcuk Thanks, however I believe that post is about dealing with print statements during a tqdm loop which is a different issue from mine (I'm also getting newlines for each update but I don't have any print statements). – user58338 Jan 18 '21 at 01:22
  • @GyuHyeonChoi ideally I'd like to still see a progress bar - just without each update appearing on a new line. – user58338 Jan 18 '21 at 01:23
  • @user58338 From the file? Does the file only contain progress bar or you write something else? – ghchoi Jan 18 '21 at 01:25
  • @GyuHyeonChoi hi, yes from the file. Since I'm redirecting all outputs to this file it would contain all my print statements from the program. – user58338 Jan 18 '21 at 01:31
  • I tried to reproduce from my local, however, only one line contained "100%" was written. Are you training a neural network on a cloud? – ghchoi Jan 18 '21 at 01:40
  • @GyuHyeonChoi thanks for trying to reproduce it! I'm not sure why you're only seeing one line (maybe your code only took one iteration?) Here's a simple example which should reproduce the issue: `from tqdm import tqdm; i = 0; for j in tqdm(range(100000000)): i += 1;` and then running it as `python tqdm_test.py &> out.txt`. In out.txt I then see an output similar to that in my post. Edit: comment formatting – user58338 Jan 18 '21 at 01:45
  • Wouldn't the whole point of writing progess to a `.txt` file be to show the progress over time (hence many lines)? Otherwise you'll just write one line with 100% completion in the file. – Jacob K Jan 18 '21 at 03:46
  • @JacobK I'm usually monitoring the file as the script runs so a live update of the bar would be useful for me. – user58338 Jan 18 '21 at 04:18
  • @user58338 did you ever solve this? – SergioGM Aug 04 '21 at 06:49
  • I did not :(. My 'solution' is to redirect the tqdm output to its own file and then the regular print/debugging statements etc. to another. – user58338 Aug 05 '21 at 17:03

0 Answers0