Welcome, guest | Sign In | My Account | Store | Cart

It's a simple function to print a progress bar under cli. When you call the function you have to indicate the max level that indicates 100% and what has been done considering the max level.

Python, 34 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/python
# Program to delete *.info files
#
# Copyleft  2011  Samuele Millevolte
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# print progress bar given the max level and what has been done
# max: it indicates the 100% level
# done: it indicates  ( done <= max )
def print_progress_bar(max, done):
	sys.stdout.write ("\r")
	sys.stdout.write ("[")
	x = int((done/max)*100)
	inner_bar = ['#' for i in range(x/10)] + [" " for i in range(10 - (x/10))]
	sys.stdout.write("".join(inner_bar))
	sys.stdout.write("]")
	sys.stdout.write(" "+str(x))
	sys.stdout.write("%")
	sys.stdout.flush()
	time.sleep(1) # only for test
	

# test code
if __name__ == '__main__':
	for i in range(1,101):
		print_progress_bar(100.0, float(i))

1 comment

Roger 11 years, 10 months ago  # | flag

someone forgot to 'import'

thanks for testing your own code !

Created by Samuele Millevolte on Mon, 21 Nov 2011 (GPL3)
Python recipes (4591)
Samuele Millevolte's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks

  • Licensed under the GPL 3
  • Viewed 6720 times
  • Revision 2 (updated 12 years ago)