0

I want to use the Python Module Progressbar.

In my case I want to search for a word in a large list. This works. So I thought a progressbar would be nice. I imported the module and tried it out:

path = str(raw_input(PATH))
word = str(raw_input(WORD))
widgets = ['Suche Wort: ', pb.Percentage(), ' ',  
            pb.Bar(marker=pb.RotatingMarker()), ' ', pb.ETA()]
timer = pb.ProgressBar(widgets=widgets, maxval=10).start()
loc = -1

with open(path) as f:
    for n in range(0,10):
        timer.update(n)
        for i, line in enumerate(f):
            if word in line:
                timer.update(n)
                loc = i
                break
    timer.finish()

But there's a problem.. the progressbar stops at 0%.When the whole loop is finished it jumps to 100%. Why?! I don't understand it.

Thank you in advance :)!

Lucas
  • 154
  • 1
  • 12

1 Answers1

0

I suspect that you are trying to increment the timer 10% at a time. You have one small error, though. The order between reading from f and when you're updating your timer.

with open(path) as f:
    for n in range(0,10):
        timer.update(n)
        for i, line in enumerate(f):
            ...
    timer.finish()

The first time the loop runs, you call timer.update(0). Then, you read all items from the file. Actually, you start at the current position in the file, and read line by line until there are no more lines in the file.

The next iteration, you update the progress bar: timer.update(1). Then, you read all items from the file, starting where the file descriptor is pointing, i.e. at the end of the file. Reading from the end of the file to the end of the file takes no time at all, so you call timer.update(2) in no time, all the way to the end.

You have arbitrarily chosen 10 steps for your progress bar, which I suspect is not related to the number of lines in f. If you know the number of lines in f, you can just update the progress bar on each iteration, or every 1000 iterations (if i % 1000 == 0:). If you don't, you'll have to figure out how many bytes you've read so far (e.g. by counting the length of the lines you read), and look up how big the file is, and use those numbers as the basis for your progress bar.

Filip Haglund
  • 13,919
  • 13
  • 64
  • 113