Skip to content

Instantly share code, notes, and snippets.

@Spacerat
Created February 2, 2014 19:04
Show Gist options
  • Save Spacerat/8773118 to your computer and use it in GitHub Desktop.
Save Spacerat/8773118 to your computer and use it in GitHub Desktop.
Delete the newest of files with the same name except for different extensions.
import os, time
import os.path
for path, dirs, files in os.walk('./'):
dups = {}
for x in files:
name, ext = os.path.splitext(x)
fullpath = path+'/'+x
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(fullpath)
if not name in dups:
dups[name] = [(name, ext, time.ctime(mtime))]
else:
dups[name].append([name, ext, time.ctime(mtime)])
dups[name] = sorted(dups[name], key=lambda x: x[2])
delname, delext, deldate = dups[name][0]
delpath = path+'/'+delname+delext
os.remove(delpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment