top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Showing Progress Bar while program is in execution using python

+1 vote
1,334 views

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.

posted Nov 23, 2013 by Garima Jain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

Ahmed 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()
answer Nov 23, 2013 by anonymous
+1 vote

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")
answer Nov 23, 2013 by Ahmed Patel
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.
Similar Questions
0 votes

I am running a python script and it will create a file name like filename0.0.0 and If I run it again then new file will create one more like filename0.0.1......
my code is-

i = 0
for i in range(1000):
 try:
 with open('filename%d.%d.%d.json'%(0,0,i,)): pass
 continue
 except IOError:
 dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')
 break

But It will take more time after creating many files, So i want to store value of last var "i" in a variable so that when I run my script again then I can use it. for example-
my last created file is filename0.0.27 then it should store 27 in a variable and when i run again then new file should be created 0.0.28 according to last value "27", so that i could save time and it can create file fast..

+2 votes

How to create a program similar to paint, but the difference would be that the cursor would be always in the middle and the canvas moves or the camera is always fixed on the cursor as it moves around the canvas. And the canvas should be infinite. What would be reasonable to use?

In addition, i want it to draw a line without me having to press a button, just move the mouse.

...