I’m reading lines form in a text file and then performing actions per line. Due to the size of the text file and the time of each action 500 => seconds. I would like to be able to view the progress but not sure where to start.
Here is an example script I’m using, how would write that for this?
import os
tmp = "test.txt"
f = open(tmp,'r')
for i in f:
ip = i.strip()
os.system("ping " + ip + " -n 500")
f.close()
test.txt:
10.1.1.1
10.1.1.2
10.2.1.1
10.2.1.1
Here’s a handy module:
progress_bar.It’s short and simple enough; read the source for ideas on implementing your own.
Here’s a very simple piece of code that, I hope, makes things clearer:
As others have observed, you still need to know the total number of lines in the file in order to have a very meaningful progress bar. The most straightforward way to do that is to read the whole file to get a line count; but that’s rather wasteful.
Incorporating this into a simple class isn’t too hard… now you can create the progress bar and
update()it whenever the value of interest changes.