Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding most recently edited file in 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.

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.

like image 670
zztpp5521 Avatar asked Apr 28 '10 15:04

zztpp5521


People also ask

How do I get the current directory in Python?

To find the current working directory in Python, use os. getcwd() , and to change the current working directory, use os. chdir(path) .


1 Answers

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
like image 152
Alve Avatar answered Oct 01 '22 02:10

Alve