How to get progress in python script.

Oscar Benjamin oscar.j.benjamin at gmail.com
Sun Sep 30 07:44:01 EDT 2012


On 28 September 2012 17:26, Rolando Cañer Roblejo
<rolando.caner at gmail.com>wrote:

> Hi all,
>
> Please, I need you suggest me a way to get statistics about a progress of
> my python script. My python script could take a lot of time processing a
> file, so I need a way that an external program check the progress of the
> script. My first idea was that the python script write a temp file showing
> the progress and the external program can check that file, but I think
> might happen file read/write locking issues.
>

Why does it need to be an external program?

I often write python scripts that run in the terminal and spend a long time
processing a file or list of files. The simply way to report progress in
that case is to just print something out every now and again. When I'm
feeling extravagant I use the progressbar library from PyPI:
http://pypi.python.org/pypi/progressbar/

With progressbar you can get a very nice display of current progress, ETA,
etc:

'''
import os.path
import sys

from progressbar import ProgressBar, Percentage, Bar, ETA

def start(text, size):
    widgets = [text + ' ', Percentage(), ' ', Bar(), ETA()]
    return ProgressBar(widgets=widgets, maxval=size).start()

def process_file(path):
    pbar = start(os.path.basename(path), os.path.getsize(path))
    with open(path) as fin:
        for line in fin:
            pbar.update(pbar.currval + len(line))
            words = line.split()
    pbar.finish()

for path in sys.argv[1:]:
    process_file(path)
'''

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120930/53a937e7/attachment.html>


More information about the Python-list mailing list