import os #replaces all occurences of "pattern" with "replace" in all files in all subfolders pattern="PndTpc" replace="Tpc" root="." filetypes=[".C", ".py", ".cxx",".h",".c",".cc",".hh",".par",".dat",".txt"] for dir, subdirs, names in os.walk( root ): for name in names: if "./rave" not in root: if os.path.splitext(name)[-1] in filetypes: path = os.path.join( dir, name ) print "\n\n"+ path+"\n" text = open( path ).read() if pattern in text: open( path, 'w' ).write(text.replace( pattern, replace ) ) # print text.replace( pattern, replace )