import os import re from os.path import walk for root, dirs, files in os.walk("/home/noa/Desktop/codes"): for name in dirs: re.search("dbname=noa user=noa", "dbname=masi user=masi") // I am trying to replace here a given match in a file #!/usr/bin/python import os import re import sys # list of extensions to replace replace_extensions = [] # example: uncomment next line to only replace *.c, *.h, and/or *.txt # replace_extensions = [".c", ".h", ".txt"] def try_to_replace(fname): if replace_extensions: return fname.lower().endswith(replace_extensions) return True def file_replace(fname, pat, s_after): # first, see if the pattern is even in the file. with open(fname) as f: if not any(re.search(pat, line) for line in f): return # pattern does not occur in file so we are done. # pattern is in the file, so perform replace operation. with open(fname) as f: out_fname = fname + ".tmp" out = open(out_fname, "w") for line in f: out.write(re.sub(pat, s_after, line)) out.close() os.rename(out_fname, fname) def mass_replace(dir_name, s_before, s_after): pat = re.compile(s_before) for dirpath, dirnames, filenames in os.walk(dir_name): for fname in filenames: if try_to_replace(fname): fullname = os.path.join(dirpath, fname) file_replace(fullname, pat, s_after) if len(sys.argv) != 4: u = "Usage: mass_replace n" sys.stderr.write(u) sys.exit(1) mass_replace(sys.argv[1], sys.argv[2], sys.argv[3]) import os def recursive_replace( root, pattern, replace ) for dir, subdirs, names in os.walk( root ): for name in names: path = os.path.join( dir, name ) text = open( path ).read() if pattern in text: open( path, 'w' ).write( text.replace( pattern, replace ) ) find /home/noa/Desktop/codes -type f -print0 | xargs -0 sed --in-place "s/dbname=noa user=noa/dbname=masi user=masi" import os, fnmatch def findReplace(directory, find, replace, filePattern): for path, dirs, files in os.walk(os.path.abspath(directory)): for filename in fnmatch.filter(files, filePattern): filepath = os.path.join(path, filename) with open(filepath) as f: s = f.read() s = s.replace(find, replace) with open(filepath, "w") as f: f.write(s) findReplace("some_dir", "find this", "replace with this", "*.txt")