Created
February 11, 2015 16:07
-
-
Save AdotDdot/374c722cad8a28a7e452 to your computer and use it in GitHub Desktop.
replace dd month dates with mmdd dates in filenames
This file contains hidden or 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
#!/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