Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get second newest file in a folder

I am trying to get most recent and second most recent file in a directory. here is my code

import glob
import os

path = r'C:\temp\Processed\*'
list_of_files = glob.glob(path) # * means all if need specific format then *.csv
sorted_files = sorted(list_of_files, key=os.path.getctime)
print sorted_files[-1]
print sorted_files[-2]

Reference: Second newest file

latest_file[-1] returns the newest file (170608_191655__Equity_Watched.csv), but latest_file[-2] gives me a 170607_082445__Equity_Watched.csv which is not the second most recent. I was expecting to get 170607_231353__Equity_Watched.csv.

enter image description here

Any idea what am I doing wrong?

like image 572
ProgSky Avatar asked Dec 24 '22 17:12

ProgSky


1 Answers

Looks like you actually want getmtime, not getctime (since you're showing us a screenshot showing modification times).

like image 89
Stefan Pochmann Avatar answered Dec 31 '22 19:12

Stefan Pochmann