measure progress (time left) while os.listdir is g

2019-05-28 06:16发布

问题:

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.