""" Run this in the directory above your data directory. TEST THIS ON A COPY FIRST -- I'M NOT GUNNA TAKE THE BLAME IF IT DELETES EVERYTHING YOU OWN!!! """ import os,re,sys,string for root,dirs,files in os.walk( 'data' ): for myfile in files: m = re.compile( '.+\.([^.]+)$' ).search(myfile) if not m or not m.group(1) == "txt": continue entry_loc = root + "/" + myfile atime = os.path.getatime(entry_loc) mtime = os.path.getmtime(entry_loc) directory = os.path.split( os.path.dirname(entry_loc) )[1] f = file(entry_loc,'r') contents = f.read() contents = re.sub( '^(.+)\n(.*)', '\g<1>\n#tags %s\n\g<2>' % ( directory ), contents ) f.close() f = file(entry_loc,'w+') f.write(contents) f.close() os.utime(entry_loc, (atime,mtime))