Python progress bar with percentage, python percentage
You can usually see a progress bar when installing programs or downloading files, prompting you of the progress of the current task. In fact, it is very easy to implement this function in python. The following is the specific code. In practical applications, you can make modifications as required! For example, the example uses time. the sleep () method controls the time delay based on the actual running time. Similarly, you can also display the actual progress ratio in the percentage of progress, instead of the auto-increment percentage of the machine in the example.
import sysimport timedef view_bar(num, total): rate = num / total rate_num = int(rate * 100) r = '\r[%s%s]%d%%' % ("="*num, " "*(100-num), rate_num, ) sys.stdout.write(r) sys.stdout.flush()if __name__ == '__main__': for i in range(0, 101): time.sleep(0.1) view_bar(i, 100)
I will share with you a method.
import hashlib a = "a test string"print hashlib.md5(a).hexdigest()print hashlib.sha1(a).hexdigest()print hashlib.sha224(a).hexdigest()print hashlib.sha256(a).hexdigest()print hashlib.sha384(a).hexdigest()print hashlib.sha512(a).hexdigest()
Let's look at a complicated function.
#!/usr/bin/env python#-*- coding:utf-8 -*-import threadingimport time'''class Demo: def __init__(self,thread_num=5): self.thread_num=thread_num def productor(self,i): print "thread-%d start" %i def start(self): threads=[] for x in xrange(self.thread_num): t=threading.Thread(target=self.productor,args=(x,)) threads.append(t) for t in threads: t.start() for t in threads: t.join() print 'all thread end' demo=Demo()demo.start()'''thread_num=10def productor(i): print "thread-%d start" %i time.sleep(2)def start(): threads=[] for x in range(thread_num): t=threading.Thread(target=productor,args=(x,)) threads.append(t) for t in threads: t.start() for t in threads: t.join() print 'all thread end'start()
#! /Usr/bin/env python #-*-coding: UTF-8-*-import paramikoimport sysprivate_key = paramiko. RSAKey. from_private_key_file ('/root /. ssh/id_rsa ') # create an SSH object ssh = paramiko. SSHClient () # Allow connection to host ssh that is not in the know_hosts file. set_missing_host_key_policy (paramiko. autoAddPolicy () t = paramiko. transport ('vm _ 000000', 22) # connect to the server t. connect (username = 'root', pkey = private_key) ssh. connect (hostname = 'vm _ 000000', port = 22, username = 'root', pkey = private_key) # Run the sftp = paramiko command. SFTPClient. from_transport (t) stdin, stdout, stderr = ssh.exe c_command ('df ') # obtain the command result = stdout. read () print resultdef progress_bar (transferred, toBeTransferred, suffix = ''): # print" Transferred: {0} \ tOut of: {1 }". format (transferred, random) bar_len = 60 filled_len = int (round (bar_len * transferred/float (toBeTransferred) percents = round (100.0 * transferred/float (random), 1) bar = '* filled_len +'-'* (bar_len-filled_len) sys. stdout. write ('[% s] % s... % s \ R' % (bar, percents, '%', suffix) sys. stdout. flush () sftp. put ("/tmp/134", "/tmp/134", callback = progress_bar) # for filename in filenames: # sftp. put (OS. path. join (dirpath, filename), # OS. path. join (remote_path, filename), # callback = self. progress_bar) # print "upload % s/% s" % (remote_path, filename) + '\ t' +' ['+ green ("success ") + '] 'ssh. close ()
The above is all the content in this article. Do you have a new understanding of the progress bar with percentage in Python? I hope you will like it.