使用 Python,如何根据文件系统的搜索获取文件信息对象的数组?

发布于 2024-08-09 15:28:24 字数 394 浏览 2 评论 0原文

目前,我有一个运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

维持三分热 2024-08-16 15:28:24
import os, time

allfiles = []
now = time.time()

# walk will return triples (current dir, list of subdirs, list of regular files)
# file names are relative to dir at first
for dir, subdirs, files in os.walk("/storage/disk-1/Media/Video/TV"):
    for f in files:
        if not f.endswith(".avi"):
            continue
        # compute full path name
        f = os.path.join(dir, f)
        st = os.stat(f)
        if st.st_mtime < now - 3600*24*7:
            # too old
            continue
        allfiles.append((f, st))

这将返回 find 也返回的所有文件,作为对列表(文件名,统计结果)。

import os, time

allfiles = []
now = time.time()

# walk will return triples (current dir, list of subdirs, list of regular files)
# file names are relative to dir at first
for dir, subdirs, files in os.walk("/storage/disk-1/Media/Video/TV"):
    for f in files:
        if not f.endswith(".avi"):
            continue
        # compute full path name
        f = os.path.join(dir, f)
        st = os.stat(f)
        if st.st_mtime < now - 3600*24*7:
            # too old
            continue
        allfiles.append((f, st))

This will return all files that find also returned, as a list of pairs (filename, stat result).

迎风吟唱 2024-08-16 15:28:24

查看模块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.

烂柯人 2024-08-16 15:28:24
  • 您可以通过“子流程”模块使用“查找”。
  • 然后,使用“split”字符串函数来剖析每一行
  • 对于每个文件,使用操作系统模块(例如getmtime等)来获取文件信息

  • 使用“walk”和“glob”模块来获取对象中的文件路径
  • You can use "find" through the "subprocess" module.
  • Afterwards, use the "split" string function to dissect each line
  • For each file, use the OS module (e.g. getmtime etc.) to get file information

or

  • Use the "walk" and "glob" modules to get the file paths in objects
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文