Skip to content

Instantly share code, notes, and snippets.

@maneeshdisodia
Forked from benhosmer/time-files-modified.py
Created September 28, 2021 08:29
Show Gist options
  • Save maneeshdisodia/6fd83c200053395a90ee6ea4790c046a to your computer and use it in GitHub Desktop.
Save maneeshdisodia/6fd83c200053395a90ee6ea4790c046a 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment