使用 Python,如何根据文件系统的搜索获取文件信息对象的数组?
目前,我有一个运行 find
命令的 bash 脚本,如下所示:
find /storage/disk-1/Media/Video/TV -name *.avi -mtime -7
这会获取过去 7 天内添加到我的系统中的电视节目列表。然后我继续创建一些符号链接,以便我可以观看最新的电视节目。
我想用 Python 重新编码,但我有几个问题,我似乎可以找到使用 Google 的答案(也许我没有寻找正确的东西)。我认为总结这一点的最好方法是问一个问题:
如何在我的文件系统上执行搜索(我应该调用 find
吗?),它为我提供了一个文件信息对象数组(包含修改日期、文件名等)以便我可以根据日期和其他此类内容对它们进行排序?
Currently I have a bash script which runs the find
command, like so:
find /storage/disk-1/Media/Video/TV -name *.avi -mtime -7
This gets a list of TV shows that were added to my system in the last 7 days. I then go on to create some symbolic links so I can get to my newest TV shows.
I'm looking to re-code this in Python, but I have a few questions I can seem to find the answers for using Google (maybe I'm not searching for the right thing). I think the best way to sum this up is to ask the question:
How do I perform a search on my file system (should I call find
?) which gives me an array of file info objects (containing the modify date, file name, etc) so that I may sort them based on date, and other such things?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这将返回 find 也返回的所有文件,作为对列表(文件名,统计结果)。
This will return all files that find also returned, as a list of pairs (filename, stat result).
查看模块os:os.walk是遍历文件系统的函数,os.path是提供文件mtime和其他文件信息的模块。 os.path 还定义了很多用于解析和分割文件名的函数。
同样有趣的是,模块 glob 定义了一个用于“通配”字符串(使用 unix 通配符规则匹配字符串)的函数
,由此构建匹配某些标准的文件列表应该很容易。
look into module os: os.walk is the function which walks the file system, os.path is the module which gives the file mtime and other file informations. also os.path defines a lot of functions for parsing and splitting filenames.
also of interest, module glob defines a functions for "globbing" strings (matching a string using unix wildcards rules)
from this, building a list of file matching some criterion should be easy.
或
or