Created
February 2, 2014 19:04
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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