简体   繁体   English

在 Python 中递归生成文件夹中所有文件的绝对路径的最快方法?

[英]Fastest way to generate the absolute paths of all the files in a folder recursively in Python?

My requirement is to generate a list of the absolute path of all the files in a Windows folder recursively.我的要求是递归生成 Windows 文件夹中所有文件的绝对路径列表。

I have tried glob.glob and glob.iglob but both take around 1.5 hours to generate the list.我试过 glob.glob 和 glob.iglob,但都需要大约 1.5 小时才能生成列表。

Now it wouldn't be a problem if it was a one-off situation.现在如果是一次性的情况就不成问题了。 This program would run daily - and this is where the problem starts.该程序将每天运行 - 这就是问题开始的地方。

After the first time run, I would only need to get the list of the latest modified files (after the last run, using the timestamp of that run).第一次运行后,我只需要获取最新修改文件的列表(在上次运行后,使用该次运行的时间戳)。 This is so that even if the first run takes 1.5 hours, the daily run should take a few minutes at max.这样一来,即使第一次运行需要 1.5 小时,每天的运行也最多需要几分钟。 Now there is no way I can get the timestamp of each individual file without going through each of the files which means I would need to go through the entire folder regardless and check for the timestamp on each of those.现在我无法在不遍历每个文件的情况下获取每个文件的时间戳,这意味着我需要 go 遍历整个文件夹,并且检查每个文件的时间戳。

Can I optimize this in any way?我可以以任何方式优化它吗? Sure ideally, the data would be arranged by date but it's not the case.当然,理想情况下,数据会按日期排列,但事实并非如此。

I'm not sure if there are pure Python approaches that will be faster.我不确定是否有更快的纯 Python 方法。 You basically want to have a service running that keeps track of changes in real-time.您基本上希望运行一个服务来实时跟踪更改。 In principle you could put the whole folder under version control (Git), and check the pending changes once per day.原则上,您可以将整个文件夹置于版本控制 (Git) 之下,并每天检查一次未决更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取文件夹中所有文件的绝对路径,而无需遍历子文件夹 - Getting the absolute paths of all files in a folder, without traversing the subfolders 在 python 中递归搜索文件的最快方法是什么? - What's the fastest way to recursively search for files in python? 使用Python递归获取所有文件(具有绝对路径)的列表及其uid - Getting a list of all files (with absolute path) recursively and their uid with Python 在 Python 3 中获取目录中所有文件的路径(但不是绝对路径) - Getting paths to all files in a directory (but not absolute path) in Python 3 如何递归地将子目录中的所有文件移动到主文件夹? [Python] - How to recursively move all files inside subdirectories to main folder? [python] Python脚本以递归方式重命名文件夹和子文件夹中的所有文件 - Python script recursively rename all files in folder and subfolders 在python中使用networkx以有效的方式生成所有路径 - Generate all paths in an efficient way using networkx in python 获取目录中所有文件的绝对路径 - Get absolute paths of all files in a directory 在 python 中大量删除所有文件的最快方法? - Fastest way to delete all files in large volume in python? Python-将相对路径转换为绝对路径的任何方法? - Python - Any way to turn relative paths into absolute paths?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM