Skip to content

Instantly share code, notes, and snippets.

@benhosmer
Created January 25, 2013 14:12
Show Gist options
  • Save benhosmer/4634721 to your computer and use it in GitHub Desktop.
Save benhosmer/4634721 to your computer and use it in GitHub Desktop.
Find the oldest and newest file in a directory and sort them.
#!/usr/bin/env python
import os
path = 'data'
os.chdir(path)
files = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
oldest = files[0]
newest = files[-1]
print "Oldest:", oldest
print "Newest:", newest
print "All by modified oldest to newest:", files
@anilkulkarni87
Copy link

anilkulkarni87 commented Jan 23, 2018

Maybe a step to handle if there are no files in the path . Or if that path is incorrect. Otherwise this is exactly what i was looking for.

@chaudhs
Copy link

chaudhs commented Mar 2, 2020

This worked for me. Thank you so much.

@ethanhunt124
Copy link

Thanks benhosmer. It helped.

@dpwendel
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment