#!/usr/bin/env python """ Change all files in this directory. """ from os import listdir, rename, remove files_to_ignore = ['change.py'] directory = "." oldtext = "is" newtext = "was" print "Replacing '%s' with '%s' in directory '%s'." \ % (oldtext, newtext, directory) for file in listdir(directory): if file in files_to_ignore: continue original = file + ".orig" rename(file, original) input = open(original, "r") output = open(file, "w") print " Working on '%s'; original saved as '%s'." % (file, original) for line in input.readlines(): newline = line.replace(oldtext, newtext) output.write(newline) # # Also look at 're' regular expression package. # from re import sub # newstring = sub( pattern, replacement, string) # pattern is a regular expression : # . any single character # * any sequence of preceding reg.exp. # () grouping # many other bells and whistles #