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
.
Any idea what am I doing wrong?
Looks like you actually want getmtime
, not getctime
(since you're showing us a screenshot showing modification times).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With