在os.listdir生成列表时测量进度(剩余时间)(Python) [英] measure progress (time left) while os.listdir is generating a list (Python)

查看:198
本文介绍了在os.listdir生成列表时测量进度(剩余时间)(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python 2.7中,我使用os.listdir生成文件夹中文件的列表.文件很多,我与该文件夹的连接速度很慢,因此最多可能需要30秒才能完成.这是一个示例:

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'

这是针对我正在使用的Tkinter GUI的,我想制作一个状态栏,以显示此步骤需要花费很长时间(约30秒)的时间或百分比.

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).

有没有一种方法可以显示完成file_list = os.listdir(dir_path)步骤所需的剩余时间或剩余百分比?

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

推荐答案

您应该使用仅在Python 3中可用的scandir.

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

但是有一个后端口.

要了解该问题,请阅读 PEP 471 .

To understand the problem, read PEP 471.

这篇关于在os.listdir生成列表时测量进度(剩余时间)(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆