Skip to content

Instantly share code, notes, and snippets.

@AdotDdot
Created February 11, 2015 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdotDdot/374c722cad8a28a7e452 to your computer and use it in GitHub Desktop.
Save AdotDdot/374c722cad8a28a7e452 to your computer and use it in GitHub Desktop.
replace dd month dates with mmdd dates in filenames
#!/usr/bin/env python3
#usage: python3 ddmonth-mmdd.py rootdir
import re
import os
from sys import argv
dtregexp = re.compile("(?P<d>\d\d?)\s?(?P<m>[A-Za-z]+)")
mleg = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"]
def applyall(root):
for rdir, sdirs, files in os.walk(root):
for f in files:
chname(os.path.join(rdir, f))
def chname(fname):
mt = re.search(dtregexp, fname)
if not mt:
print("%s NO"%fname)
return 0
dtstring = fname[mt.start():mt.end()]
dct = mt.groupdict()
try:
os.rename(fname, fname.replace(dtstring, "%d%s"%(mleg.index(dct["m"].lower())+1, dct["d"].rjust(2, "0"))))
print("%s OK"%fname)
except ValueError:
print("%s NO"%fname)
if __name__ == "__main__":
try:
applyall(argv[1])
except IndexError:
print("dir?")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment