Python: Replace a string at all occurrence in all files in a directory, including files in sub-directories

Now this is for all super lazy out there. Replace a string at all occurrence in all files in a directory, including files in sub-directories. Works with python 2.x.

--------------------------------------------------------------------------------------
import os
import fileinput
import sys
#from fnmatch import fnmatch

root = '/root/mypython/log'
names = []
for path, subdirs, files in os.walk(root):
    for name in files:
        filenames = os.path.join(path, name)
        names.append(filenames)
for item in names:
    for line in fileinput.input(item, inplace=True):
        line = line.replace('linux', 'android')
        sys.stdout.write(line)
    print 'String replaced in ' + item

Comments