在python中查找最近编辑的文件 [英] Finding most recently edited file in python

查看:112
本文介绍了在python中查找最近编辑的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组文件夹,并且我希望能够运行一个功能,该功能可以找到最近编辑的文件,并告诉我文件名和它所在的文件夹.

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.

文件夹布局:

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.

推荐答案

您应该查看操作系统. walk 函数以及 os.stat ,它可以让您执行某些操作像:

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

这篇关于在python中查找最近编辑的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆