Python下载脚本

时间:2019-01-19 00:16:08

标签: python python-3.x

运行此命令时,进度%向后,是否有人知道如何使其开头为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 个答案:

答案 0 :(得分:2)

只需使用(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%