Finding most recently edited file in python

后端 未结 8 1932
说谎
说谎 2021-02-02 02:57

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.

F

8条回答
  •  说谎
    说谎 (楼主)
    2021-02-02 03:23

    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
    

提交回复
热议问题