Search code examples
pythontqdm

How do I update a tqdm progress bar with line size?


I'm trying to load a file in Python2.7 (Ubuntu 16.04), and display the current progress with tqdm:

from tqdm import tqdm
import os
with open(filename, 'r') as f:
    vectors = {}
    tq = tqdm(f, total=os.path.getsize(filename))
    for line in tq:
        vals = line.rstrip().split(' ')
        vectors[vals[0]] = np.array([float(x) for x in vals[1:]])
        tq.update(len(line))

It's not working though, the ETA is way too big. It kind of follows this example, but I was trying to do as said in the comment.


Solution

  • I found the key was not passing the file object as the 'iterable' argument for tqdm and manually managing the updates instead:

    from tqdm import tqdm
    import os
    
    filename = '/home/nate/something.txt'
    
    with open(filename, 'r') as f:
        # unit='B' and unit_scale just prettifies the bar a bit
        tq = tqdm(total=os.path.getsize(filename), unit='B', unit_scale=True)
        for line in f:
            tq.update(len(line))