【发布时间】:2021-04-17 12:18:54
【问题描述】:
我在递归目录树时使用 tqdm。我不知道我将使用的路径数,我不想在我做工作之前建立那个列表只是为了得到一个准确的总数,我宁愿让它更新进度条为它会继续。
我发现我可以很好地使用“reset(total=new_total)”,但这也会重置时间。有没有办法让我保持时间,但只需将总数设置为新的?
【问题讨论】:
我在递归目录树时使用 tqdm。我不知道我将使用的路径数,我不想在我做工作之前建立那个列表只是为了得到一个准确的总数,我宁愿让它更新进度条为它会继续。
我发现我可以很好地使用“reset(total=new_total)”,但这也会重置时间。有没有办法让我保持时间,但只需将总数设置为新的?
【问题讨论】:
这里是tqdm包内的reset函数定义:
def reset(self, total=None):
"""
Resets to 0 iterations for repeated use.
Consider combining with `leave=True`.
Parameters
----------
total : int, optional. Total to use for the new bar.
"""
self.last_print_n = self.n = 0
self.last_print_t = self.start_t = self._time()
if total is not None:
self.total = total
self.refresh()
您需要的不是更新self.last_print_t 和self.start_t 的值,而是更新total
您应该执行以下操作,而不是调用t.reset(total=new_total):
t.total = new_total
t.refresh()
【讨论】: