python sort by time directory of the file implementation method

  • 2020-12-20 03:42:19
  • OfStack

Without further ado, just code:

python folder traversal, file operations, access to file modification creation time can go to the web reference other articles.

Such as:


os.path.getmtime()  The function gets the last modified time of the file  
os.path.getctime()  The getFile () function is the last time the file was created 

def get_file_list(file_path):
  dir_list = os.listdir(file_path)
  if not dir_list:
    return
  else:
    #  Notice, it's used here lambda Expression that sorts the files in ascending chronological order from last modification 
    # os.path.getmtime()  The function gets the last modified time of the file 
    # os.path.getctime()  The getFile () function is the last time the file was created 
    dir_list = sorted(dir_list, key=lambda x: os.path.getmtime(os.path.join(file_path, x)))
    # print(dir_list)
    return dir_list

Related articles: