Python CLI(命令行)进度条

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python CLI(命令行)进度条相关的知识,希望对你有一定的参考价值。

This is a function that will show a progress bar of the given percentage. Useful when performing time consuming tasks.
  1. import sys
  2. import time
  3. import math
  4.  
  5. # Output example: [======= ] 75%
  6.  
  7. # width defines bar width
  8. # percent defines current percentage
  9. def progress(width, percent):
  10. marks = math.floor(width * (percent / 100.0))
  11. spaces = math.floor(width - marks)
  12.  
  13. loader = '[' + ('=' * int(marks)) + (' ' * int(spaces)) + ']'
  14.  
  15. sys.stdout.write("%s %d%% " % (loader, percent))
  16. if percent >= 100:
  17. sys.stdout.write(" ")
  18. sys.stdout.flush()
  19.  
  20. # Simulate doing something...
  21. for i in xrange(100):
  22. progress(50, (i + 1)) # +1 because xrange is only 99
  23. time.sleep(0.1) # Slow it down for demo

以上是关于Python CLI(命令行)进度条的主要内容,如果未能解决你的问题,请参考以下文章

python Python cli进度条

python Python cli进度条

2019年的代码都写完了吗?不如做个Python进度条看看还剩多少

Python进度条程序展示方案

命令行模拟进度条

VSCode自定义代码片段——cli的终端命令大全