Finding most recently edited file in python

后端 未结 8 1927
说谎
说谎 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:36

    You can use

    os.walk
    

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

    0 讨论(0)
  • 2021-02-02 03:36

    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?

    0 讨论(0)
提交回复
热议问题