Showing Progress Bar

H

Himanshu Garg

I want to show simple dots while my program copies the files. I have found the code like:

for i in range(10):
print '.',
time.sleep(1)

But this will execute ten times as it is predefined and the task to copy will execute after or before this loop based on the location I have placed my line to copy the files using shutil.copy().

I want that the files should be copied and in parallel the progress should be shown and when the files are copied, the progress should exit.
 
N

Ned Batchelder

I want to show simple dots while my program copies the files. I have found the code like:

for i in range(10):
print '.',
time.sleep(1)

But this will execute ten times as it is predefined and the task to copy will execute after or before this loop based on the location I have placed my line to copy the files using shutil.copy().

I want that the files should be copied and in parallel the progress should be shown and when the files are copied, the progress should exit.

Stdout is buffered, meaning it will wait for a newline, or for some large amount of output, before actually outputting the characters to the screen. Use the flush method to force it to be output now. Writing to sys.stdout directly also lets you avoid the space that a print-comma gives you:

for i in range(10):
sys.stdout.write(".")
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\n")

--Ned.
 
H

Himanshu Garg

for i in range(10):
sys.stdout.write(".")
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\n")

shutil.copytree("pack", "/lxc/pack")

But Here, the loop will first print the progress dots and then it will copy the directory. But I want that these two tasks should run simultaneously.
 
F

Frank Millman

Himanshu Garg said:
I want to show simple dots while my program copies the files. I have found
the code like:

for i in range(10):
print '.',
time.sleep(1)

But this will execute ten times as it is predefined and the task to copy
will execute after or before this loop based on the location I have placed
my line to copy the files using shutil.copy().

I want that the files should be copied and in parallel the progress should
be shown and when the files are copied, the progress should exit.

Ned has shown how to view the dots as they are printed, but has not
addressed the 'parallel' aspect.

Here is one approach, which uses the threading module to print the dots in a
separate thread while the main thread does the copying.

import sys
import threading

class ProgressBar(threading.Thread):
"""
In a separate thread, print dots to the screen until terminated.
"""

def __init__(self):
threading.Thread.__init__(self)
self.event = threading.Event()

def run(self):
event = self.event # make local
while not event.is_set():
sys.stdout.write(".")
sys.stdout.flush()
event.wait(1) # pause for 1 second
sys.stdout.write("\n")

def stop(self):
self.event.set()

Before starting the copy -

progress_bar = ProgressBar()
progress_bar.start()

When the copy is finished -

progress_bar.stop()
progress_bar.join()

HTH

Frank Millman
 
C

Chris Angelico

class ProgressBar(threading.Thread):
"""
In a separate thread, print dots to the screen until terminated.
"""

It's worth noting that this, as coded, is not a progress bar but
merely an activity bar. The number of dots printed depends on the time
taken and not on the amount of "stuff" done. For a true progress bar,
you'd have to manage something through the copy process itself -
figure out how much there is to copy, figure out how many dots to
display in total (probably a fixed number, eg 50), then output a dot
every X bytes or files copied, based on that proportion. This is a lot
simpler, of course!

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top