Você pode usar o script a seguir. Chame com nomes de arquivos para testar e excluir como argumentos.
#!/usr/bin/env python
# encoding: utf-8
import os
import sys
import datetime
test_file_names = [x for x in """\
AA_XX_20130719185428.log
exec_xxx_cpb_20130712182453.log
13122013121327_AR_INC_DOBC_1.dprf.log
24122013_masterscript.LOG
""".split('\n') if x.strip()]
# appr. 2 meses
two_months = datetime.date.today() - datetime.timedelta(days=61)
testing = len(sys.argv) < 2
if testing:
file_names = test_file_names
else:
file_names = sys.argv[1:]
allow_datetime = set([
len('YYYYmmddHHMMSS'),
len('ddmmYYYYHHMMSS'),
len('ddmmYYYY'),
])
for file_name in test_file_names:
for part in os.path.splitext(os.path.basename(file_name))[0].split('_'):
if len(part) not in allow_datetime:
continue
for ch in part:
if not ch.isdigit():
break
else:
dt = part
if dt[4:6] == '20': # ddmmYYYY
yy = int(dt[4:8])
mm = int(dt[2:4])
dd = int(dt[:2])
else: # YYYYmmdd
yy = int(dt[:4])
mm = int(dt[4:6])
dd = int(dt[6:8])
try:
d = datetime.date(yy, mm, dd)
except ValueError:
print 'wrong date', yy, mm, dd
raise
if testing:
print '{:<6s} {:<40s} {}'.format(
'remove' if d < two_months else ' ', file_name, repr(d))
elif d < two_months:
print 'removing', file_name
os.remove(file_name)