<div class="gmail_quote">On 28 September 2012 17:26, Rolando Caņer Roblejo <span dir="ltr"><<a href="mailto:rolando.caner@gmail.com" target="_blank">rolando.caner@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi all,<br>
<br>
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.<br>
</blockquote><div><br></div><div>Why does it need to be an external program?</div><div><br></div><div>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:</div>
<div><a href="http://pypi.python.org/pypi/progressbar/">http://pypi.python.org/pypi/progressbar/</a></div><div><br></div><div>With progressbar you can get a very nice display of current progress, ETA, etc:</div><div><br></div>
<div>'''</div><div><div>import os.path</div><div>import sys</div><div><br></div><div>from progressbar import ProgressBar, Percentage, Bar, ETA</div><div><br></div><div>def start(text, size):</div><div> widgets = [text + ' ', Percentage(), ' ', Bar(), ETA()]</div>
<div> return ProgressBar(widgets=widgets, maxval=size).start()</div><div><br></div><div>def process_file(path):</div><div> pbar = start(os.path.basename(path), os.path.getsize(path))</div><div> with open(path) as fin:</div>
<div> for line in fin:</div><div> pbar.update(pbar.currval + len(line))</div><div> words = line.split()</div><div> pbar.finish()</div><div><br></div><div>for path in sys.argv[1:]:</div><div>
process_file(path)</div></div><div>'''</div><div><br></div><div>Oscar</div></div>