在目录中查找最新的文件而不读取其中的所有内容 [英] Find the most recent file in a directory without reading all the contents of it
问题描述
我试图在庞大的文件系统中找出最新的文件.一种方法是浏览所有目录 - 一次一个,读取其内容,选择最新的文件等.
I'm trying to find out the latest file in a huge filesystem. One way to do this is to go through all directories - one at a time, read its contents, select the latest file etc.
明显的缺点是我必须在特定目录中获取所有文件.我想知道 Python [1] 中是否有一个魔法"调用,Unix 支持它只获取目录中的最新文件.
The obvious drawback is I have to get all the files in a specific directory. I was wondering whether there was a 'magic' call in Python [1] which Unix supports to get just the latest file in a directory.
[1].我的应用程序使用 Python,但如果 stdlib 中不存在现成的解决方案,请使用系统调用提供 C(语言)替代方案.我愿意编写一个 C 扩展并完成这项工作.
[1]. My application is in Python, but if a readymade solution doesnt exist in stdlib, please provide C (lanuage) alternatives using system calls. I'm willing to write a C-extension and make this work.
谢谢
更新:我想我应该解释一下为什么 inotify 类型的解决方案对我不起作用.我只是在寻找一个使用 Python/C 的系统调用,它可以给我最新的文件.是的,可以使用 inotify(或类似的总体设置)来监控 FS 更改,但给定一个随机目录,我如何找到最新的文件是问题的本质.
update: I suppose I should offer an explanation on why an inotify type solution wont work for me. I was simply looking for a system call using Python/C which could give me the latest file. Yes, one could have inotify (or a similar overarching setup) which monitors FS changes but given a random directory how do I find the latest file is the essence of the question.
推荐答案
我不相信Unix"或 Posix 系统一般都支持独立于平台的文件系统更改通知.
I do not believe that generically 'Unix' or Posix systems support a platform independent file system change notification.
也就是说,有很多 unixy 系统可以做到:
That said, there are lots of unixy systems that do:
- OS X
- OS X
- FSEvents
- fslogger
- DTrace + syscall
- pnotify
- Kqueue
- DTrace
- 通知
- Kqueue
- Kqueue is directly supported in Python only on BSD
- Pyinotify only on Linux
- FSEvents only on Darwin / OS X
- Gamin only on Linux and BSD
其他人建议尝试解释
ls
.不要那样做.如果您觉得必须使用 Unix 工具,大多数 Unix/Linux/Posix 风格也有stat
作为实用程序.stat 实用程序 具有可配置的输出和您可以设置要解析的字段.它是 GNU 核心实用程序的一部分.Others have suggested trying to interpret
ls
. Don't do that. If you feel compelled to use a Unix tool, most Unix / Linux / Posix flavors also havestat
as a utility. The stat utility has configurable output and you can set the fields that you want to parse. It is part of the GNU core utilities.这篇关于在目录中查找最新的文件而不读取其中的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- OS X