Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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