May-02-2024, 07:59 AM
Maybe this will help with the understanding part?
from pathlib import Path import os mydir = Path('/home/pedro/tmp/') # get all files filelist = [filename for filename in mydir.iterdir() if filename.is_file()] # copy filelist old_list = filelist.copy() # sort filelist according to os.path.getmtime filelist.sort(key=os.path.getmtime, reverse=True) # compare the 2 lists for f in range(len(filelist)): print(filelist[f]) print(old_list[f])To get the actual date of modification:
from datetime import datetime def modification_date(filename): secs = os.path.getmtime(filename) # like: 1634870901.972196 dt = datetime.fromtimestamp(secs) date = dt.strftime("%A %B %Y %H:%M:%S.%f") print(f'file is: {f}') print(f'last modified time in epoch seconds: {secs}') print(f'date of last modification was: {date}\n') mydir = Path('/home/pedro/tmp/') file_gen = (filename for filename in mydir.iterdir() if filename.is_file()) for f in file_gen: modification_date(f)