10

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.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
zztpp5521
  • 101
  • 1
  • 1
  • 3

8 Answers8

21

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
Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
  • 4
    `os.path.getmtime()` is a convenient shorthand for `os.stat(full_path).st_mtime` – Thomas Wouters Apr 28 '10 at 16:45
  • 1
    I get an error when I run it from my home directory: OSError: [Errno 2] No such file or directory: './.config/chromium/SingletonLock' – armandino Apr 28 '10 at 18:22
  • 1
    @armandino: I'm guessing that the issue is that there's a lock file which is being created and then deleted very rapidly. If it was deleted after `os.walk` returned its file name, but before the call to `os.stat` then you would expect to see such an error. If you actually needed to run such a script on directories which have files being deleted in such a manner, I would recommend wrapping the call to `os.stat` in a try/except block. – Eli Courtwright Apr 28 '10 at 18:49
  • Eli, how can i find the last modified file in every subfolder and print it? – newGIS Feb 19 '17 at 07:50
10

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)
Ants Aasma
  • 53,288
  • 15
  • 90
  • 97
7

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
Alve
  • 640
  • 2
  • 9
  • 19
  • 2
    Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. [How to Answer](https://stackoverflow.com/help/how-to-answer) – Elletlar Jan 26 '19 at 09:26
4
  • 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.
YOU
  • 120,166
  • 34
  • 186
  • 219
2

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.

scob_
  • 277
  • 1
  • 11
1

You can use

os.walk

See: http://docs.python.org/library/os.html

compie
  • 10,135
  • 15
  • 54
  • 78
1

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.

Dan Head
  • 2,672
  • 1
  • 17
  • 10
1

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?

danius
  • 2,664
  • 27
  • 33
Dil Yu
  • 11
  • 2