python实现控制台输出文本进度条

代码:

import time
scale = 10
print("------执行开始------")
for i in range(scale+1): # i = 0,1,2,...,9,10
    a, b = "**" * i, ".." * (scale - i)
    c = (i/scale)*100
    print("%{:^4.0f}[{}->{}]".format(c,a,b))
    time.sleep(0.1)
print("------执行结束------")

控制台输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS F:\TEST>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '59575' '--' 'f:\TEST\1.py'
------执行开始------
% 0  [->....................]
% 10 [**->..................]
% 20 [****->................]
% 30 [******->..............]
% 40 [********->............]
% 50 [**********->..........]
% 60 [************->........]
% 70 [**************->......]
% 80 [****************->....]
% 90 [******************->..]
%100 [********************->]
------执行结束------
PS F:\TEST>

代码:

import time
for i in range(101):
    print("\r{:3}%".format(i),end="")
    time.sleep(0.05)

控制台输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS F:\TEST>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '59594' '--' 'f:\TEST\1.py'
 94%

代码:

import time
print("##"*20,end="")
time.sleep(2)
print("\r****")

控制台输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS F:\TEST>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '59611' '--' 'f:\TEST\1.py'
****####################################
PS F:\TEST> 

代码:

import time
scale = 50
print("执行开始".center(scale//2,'-'))
t = time.clock()
for i in range(scale+1):
    a = '*' * i
    b = '.' * (scale - i)
    c = (i/scale)*100
    t = time.clock()
    # print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="\n")
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="")
    time.sleep(0.05)
print("\n林祖泉:"+"执行结束".center(scale//2,'-'))

控制台输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS F:\TEST>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '59628' '--' 'f:\TEST\1.py'
-----------执行开始----------
f:\TEST\1.py:35: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
  t = time.clock()
f:\TEST\1.py:40: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
  t = time.clock()
100%[**************************************************->]2.59s
林祖泉:-----------执行结束----------
PS F:\TEST> 

代码:

import time
scale = 50
print("执行开始".center(scale//2,'-'))
start = time.perf_counter()
for i in range(scale+1):
    a = '*' * i
    b = '.' * (scale - i)
    c = (i/scale)*100
    t = time.perf_counter() - start
    # print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="\n")
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="")
    time.sleep(0.05)
print("\n林祖泉:"+"执行结束".center(scale//2,'-'))

控制台输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS F:\TEST>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '59643' '--' 'f:\TEST\1.py'
-----------执行开始----------
100%[**************************************************->]2.54s
林祖泉:-----------执行结束----------
PS F:\TEST> 

代码:

import time
scale = 50
print("执行开始".center(scale//2,'-'))
start = time.process_time()
for i in range(scale+1):
    myList = [item for item in range(1000000)]
    a = '*' * i
    b = '.' * (scale - i)
    c = (i/scale)*100
    t = time.process_time() - start
    # myList = [item for item in range(10)]
    # print(myList)
    # print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="\n")
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="") 
    time.sleep(0.05)
print("\n林祖泉:"+"执行结束".center(scale//2,'-'))

控制台输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS F:\TEST>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '59658' '--' 'f:\TEST\1.py'
-----------执行开始----------
100%[**************************************************->]5.09s
林祖泉:-----------执行结束----------
PS F:\TEST> 

全部代码:

import time
scale = 10
print("------执行开始------")
for i in range(scale+1): # i = 0,1,2,...,9,10
    a, b = "**" * i, ".." * (scale - i)
    c = (i/scale)*100
    print("%{:^4.0f}[{}->{}]".format(c,a,b))
    time.sleep(0.1)
print("------执行结束------")





import time
for i in range(101):
    print("\r{:3}%".format(i),end="")
    time.sleep(0.05)





import time
print("##"*20,end="")
time.sleep(2)
print("\r****")




import time
scale = 50
print("执行开始".center(scale//2,'-'))
t = time.clock()
for i in range(scale+1):
    a = '*' * i
    b = '.' * (scale - i)
    c = (i/scale)*100
    t = time.clock()
    # print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="\n")
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="")
    time.sleep(0.05)
print("\n林祖泉:"+"执行结束".center(scale//2,'-'))



import time
scale = 50
print("执行开始".center(scale//2,'-'))
start = time.perf_counter()
for i in range(scale+1):
    a = '*' * i
    b = '.' * (scale - i)
    c = (i/scale)*100
    t = time.perf_counter() - start
    # print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="\n")
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="")
    time.sleep(0.05)
print("\n林祖泉:"+"执行结束".center(scale//2,'-'))





import time
scale = 50
print("执行开始".center(scale//2,'-'))
start = time.process_time()
for i in range(scale+1):
    myList = [item for item in range(1000000)]
    a = '*' * i
    b = '.' * (scale - i)
    c = (i/scale)*100
    t = time.process_time() - start
    # myList = [item for item in range(10)]
    # print(myList)
    # print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="\n")
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,t),end="") 
    time.sleep(0.05)
print("\n林祖泉:"+"执行结束".center(scale//2,'-'))


控制台输出:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS F:\TEST>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.9.114305\pythonFiles\lib\python\debugpy\launcher' '59678' '--' 'f:\TEST\1.py'
------执行开始------
% 0  [->....................]
% 10 [**->..................]
% 20 [****->................]
% 30 [******->..............]
% 40 [********->............]
% 50 [**********->..........]
% 60 [************->........]
% 70 [**************->......]
% 80 [****************->....]
% 90 [******************->..]
%100 [********************->]
------执行结束------
****########################################
-----------执行开始----------
f:\TEST\1.py:35: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
  t = time.clock()
f:\TEST\1.py:40: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
  t = time.clock()
100%[**************************************************->]10.84s
林祖泉:-----------执行结束----------
-----------执行开始----------
100%[**************************************************->]2.52s
林祖泉:-----------执行结束----------
-----------执行开始----------
100%[**************************************************->]5.12s
林祖泉:-----------执行结束----------
PS F:\TEST> 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值