python脚本进度条
1. 定义进度条
from tqdm import tqdm
def tqdmWrapViewBar(*args, **kwargs): pbar = tqdm(*args, **kwargs) # make a progressbar last = [0] # last known iteration, start at 0 def viewBar(a, b): pbar.total = int(b) pbar.update(int(a - last[0])) # update pbar with increment last[0] = a # update last known iteration return viewBar, pbar # return callback, tqdmInstance
2. 实例化进度条
cbk, pbar = tqdmWrapViewBar(ascii=True, unit='b', unit_scale=True)
3. 使用进度条
def sftp_down_file(host, logfile): try: t = paramiko.Transport((host, 22)) t.connect(username="root", pkey=pkfile) sftp = paramiko.SFTPClient.from_transport(t) sftp.get(logfile, "456.txt", callback=cbk) #使用回调函数展示进度条 t.close() except Exception as e: print(e)