Python實(shí)現(xiàn)帶百分比的進(jìn)度條
大家在安裝程序或下載文件時(shí),通常都能看到進(jìn)度條,提示你當(dāng)前任務(wù)的進(jìn)度。其實(shí),在python中實(shí)現(xiàn)這個(gè)功能很簡(jiǎn)單,下面是具體代碼。在實(shí)際應(yīng)用中,你完全可以根據(jù)自己的要求進(jìn)行修改!比如,示例中是通過(guò)time.sleep()方法進(jìn)行時(shí)間延遲,你完全可以根據(jù)實(shí)際的程序運(yùn)行耗時(shí)進(jìn)行控制;同樣,在進(jìn)度百分比處,你也可以顯示實(shí)際的進(jìn)度比,而不是例子中機(jī)械的自增百分比。
import sys
import time
def 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)
再給大家分享一個(gè)方法
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()
再來(lái)一個(gè)復(fù)雜點(diǎn)的函數(shù)吧
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import threading
import 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=10
def 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 paramiko
import sys
private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
# 創(chuàng)建SSH對(duì)象
ssh = paramiko.SSHClient()
# 允許連接不在know_hosts文件中的主機(jī)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
t = paramiko.Transport(('vm_135',22))
# 連接服務(wù)器
t.connect(username='root',pkey=private_key)
ssh.connect(hostname='vm_135', port=22, username='root',pkey=private_key)
# 執(zhí)行命令
sftp = paramiko.SFTPClient.from_transport(t)
stdin, stdout, stderr = ssh.exec_command('df')
# 獲取命令結(jié)果
result = stdout.read()
print result
def progress_bar(transferred, toBeTransferred, suffix=''):
# print "Transferred: {0}\tOut of: {1}".format(transferred, toBeTransferred)
bar_len = 60
filled_len = int(round(bar_len * transferred/float(toBeTransferred)))
percents = round(100.0 * transferred/float(toBeTransferred), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %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
# print "upload %s/%s" % (remote_path, filename) + '\t' + '[' + green("success") + ']'
ssh.close()
以上就是本文的全部?jī)?nèi)容了,大家是否對(duì)使用Python實(shí)現(xiàn)帶百分比進(jìn)度條有了新的認(rèn)識(shí)了呢,希望大家能夠喜歡。
相關(guān)文章
Python正則表達(dá)式re.search()用法詳解
re是Python中最常見(jiàn)的正則表達(dá)式模塊,常用方法包括compile,match,findall,finditer,search,split,sub等,下面這篇文章主要給大家介紹了關(guān)于Python正則表達(dá)式re.search()用法詳解的相關(guān)資料,需要的朋友可以參考下2022-09-09
對(duì)python中xlsx,csv以及json文件的相互轉(zhuǎn)化方法詳解
今天小編就為大家分享一篇對(duì)python中xlsx,csv以及json文件的相互轉(zhuǎn)化方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
關(guān)于Python中幾種隊(duì)列Queue用法區(qū)別
這篇文章主要介紹了關(guān)于Python中幾種隊(duì)列Queue用法區(qū)別,queue隊(duì)列中的put()或者get()方法中都提供了timeout參數(shù),利用這個(gè)參數(shù)可以有效解決上述消除不能消費(fèi)和線(xiàn)程一直阻塞問(wèn)題,需要的朋友可以參考下2023-05-05
Python + OpenCV 實(shí)現(xiàn)LBP特征提取的示例代碼
這篇文章主要介紹了Python + OpenCV 實(shí)現(xiàn)LBP特征提取的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
用Python實(shí)現(xiàn)流星雨效果的方法詳解
這篇文章主要為大家介紹了Python實(shí)現(xiàn)流星雨效果的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助<BR>2021-12-12
使用OpenCV實(shí)現(xiàn)仿射變換—平移功能
這篇文章主要介紹了使用OpenCV實(shí)現(xiàn)仿射變換—平移功能,需要的朋友可以參考下2019-08-08
對(duì)python遍歷文件夾中的所有jpg文件的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python遍歷文件夾中的所有jpg文件的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12

