当我运行此命令时,进度百分比会倒退,有人知道如何使它从开始时的0%到完成时的100%吗?

import time

x = 25
y = x
t = 0

downloading = True
while downloading:
  time.sleep(1)
  t += 1
  x -= 1
  f = ((x/y) * 100)
  print('Time:', str(t) + ',', 'Progress: ', '{0:.2}'.format(str(f)) + '%,', 'Remaining: ' + str(x), 'MB', end="\r")

  if(x == 0):
    print('\nComplete!')
    break

最佳答案

只需使用(1-x/y)代替x/y中的f

import time

x = 25
y = x
t = 0

downloading = True
while downloading:
  time.sleep(0.01)
  t += 1
  x -= 1
  f = ((1-x/y) * 100)
  print('Time:', str(t) + ',', 'Progress: ', '{0:.3}'.format(str(f)) + '%,', 'Remaining: ' + str(x), 'MB', end="\r")

  if(x == 0):
    print('\nComplete!')
    break


另请注意,应使用'{0:.3}'.format(str(f)),以便可以正确显示100%

关于python - Python下载脚本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54262983/

10-16 15:53