measure progress (time left) while os.listdir is generating a list (Python)

青春壹個敷衍的年華 提交于 2020-01-02 14:56:53

问题


In Python 2.7, I am using os.listdir to generate a list of files in a folder. There are lots of files and my connection to the folder is slow so it can take up to 30 seconds to complete. Here is an example:

import os
import time

start_time = time.time()
dir_path = r'C:\Users\my_name\Documents\data_directory'   #example path
file_list = os.listdir(dir_path)

print 'it took', time.time() - start_time, 'seconds'

This is for a Tkinter GUI I'm working on and I would like make a status bar showing how much time or percentage is left for this step that takes a long time (about 30 seconds).

Is there a way to show the time left or percentage left to complete the file_list = os.listdir(dir_path) step???


回答1:


You ought to use scandir which is only available with Python 3.

But there is a back port.

To understand the problem, read PEP 471.



来源:https://stackoverflow.com/questions/39429978/measure-progress-time-left-while-os-listdir-is-generating-a-list-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!