在python中查找最近编辑的文件

发布于 2024-08-30 19:29:50 字数 217 浏览 2 评论 0原文

我有一组文件夹,我希望能够运行一个函数来查找最近编辑的文件并告诉我该文件的名称及其所在的文件夹。

文件夹布局:

root
    Folder A
        File A
        File B
    Folder B
        File C
        File D
etc...

任何让我开始的提示我碰壁了。

I have a set of folders, and I want to be able to run a function that will find the most recently edited file and tell me the name of the file and the folder it is in.

Folder layout:

root
    Folder A
        File A
        File B
    Folder B
        File C
        File D
etc...

Any tips to get me started as i've hit a bit of a wall.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

魔法少女 2024-09-06 19:29:50

您应该查看 os.walk 函数,以及 < a href="http://docs.python.org/library/os.html#os.stat" rel="nofollow noreferrer">os.stat,它可以让你执行以下操作:

import os

max_mtime = 0
for dirname,subdirs,files in os.walk("."):
    for fname in files:
        full_path = os.path.join(dirname, fname)
        mtime = os.stat(full_path).st_mtime
        if mtime > max_mtime:
            max_mtime = mtime
            max_dir = dirname
            max_file = fname

print(max_dir, max_file)

You should look at the os.walk function, as well as os.stat, which can let you do something like:

import os

max_mtime = 0
for dirname,subdirs,files in os.walk("."):
    for fname in files:
        full_path = os.path.join(dirname, fname)
        mtime = os.stat(full_path).st_mtime
        if mtime > max_mtime:
            max_mtime = mtime
            max_dir = dirname
            max_file = fname

print(max_dir, max_file)

它有助于将内置目录遍历包装为仅生成文件的完整路径的函数。然后,您可以使用返回所有文件的函数并选出修改时间最长的文件:

import os

def all_files_under(path):
    """Iterates through all files that are under the given path."""
    for cur_path, dirnames, filenames in os.walk(path):
        for filename in filenames:
            yield os.path.join(cur_path, filename)

latest_file = max(all_files_under('root'), key=os.path.getmtime)

It helps to wrap the built in directory walking to function that yields only full paths to files. Then you can just take the function that returns all files and pick out the one that has the highest modification time:

import os

def all_files_under(path):
    """Iterates through all files that are under the given path."""
    for cur_path, dirnames, filenames in os.walk(path):
        for filename in filenames:
            yield os.path.join(cur_path, filename)

latest_file = max(all_files_under('root'), key=os.path.getmtime)
赏烟花じ飞满天 2024-09-06 19:29:50

如果有人正在寻找一种单行方法来做到这一点:

latest_edited_file = max([f for f in os.scandir("path\\to\\search")], key=lambda x: x.stat().st_mtime).name

If anyone is looking for an one line way to do it:

latest_edited_file = max([f for f in os.scandir("path\\to\\search")], key=lambda x: x.stat().st_mtime).name
围归者 2024-09-06 19:29:50
  • 使用 os.walk 列出文件
  • 使用 os.stat 获取文件修改时间戳 (st_mtime)
  • 将时间戳和文件名放在列表中,然后按时间戳排序,最大时间戳是最近编辑的文件。
  • use os.walk to list files
  • use os.stat to get file modified timestamp (st_mtime)
  • put both timestamps and filenames in a list and sort it by timestamp, largest timestamp is most recently edited file.
冰雪之触 2024-09-06 19:29:50

对于多个文件,如果有人来这里:

import glob, os

files = glob.glob("/target/directory/path/*/*.mp4")
files.sort(key=os.path.getmtime)

for file in files:
   print(file)

这将打印 /path/ 内任何文件夹中具有 .mp4 扩展名的所有文件,以及最近修改的文件底部的路径。

For multiple files, if anyone came here for that:

import glob, os

files = glob.glob("/target/directory/path/*/*.mp4")
files.sort(key=os.path.getmtime)

for file in files:
   print(file)

This will print all files in any folder within /path/ that have the .mp4 extension, with the most recently modified file paths at the bottom.

夜空下最亮的亮点 2024-09-06 19:29:50

使用 os.path.walk()< /a> 遍历目录树和 os.stat().st_mtime< /code>获取文件的 mtime。

您传递给 os.path.walk() 的函数(visit 参数)只需要跟踪它所看到的最大 mtime 以及它在哪里看到的。

Use os.path.walk() to traverse the directory tree and os.stat().st_mtime to get the mtime of the files.

The function you pass to os.path.walk() (the visit parameter) just needs to keep track of the largest mtime it's seen and where it saw it.

荒芜了季节 2024-09-06 19:29:50

我正在使用 path = r"C:\Users\traveler\Desktop"

import os
def all_files_under(path):
   #"""Iterates through all files that are under the given path."""
   for cur_path, dirnames, filenames in os.walk(path):
      for filename in filenames:
         yield os.path.join(cur_path, filename)
latest_file = max(all_files_under('root'), key=os.path.getmtime)

我在这里缺少什么?

I'm using path = r"C:\Users\traveler\Desktop":

import os
def all_files_under(path):
   #"""Iterates through all files that are under the given path."""
   for cur_path, dirnames, filenames in os.walk(path):
      for filename in filenames:
         yield os.path.join(cur_path, filename)
latest_file = max(all_files_under('root'), key=os.path.getmtime)

What am i missing here?

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