replace ALL instances of a character with another one in all files hierarchically in directory tree
===

so the command line will look like :

./replace.py <current working directory> <before> <after>

current directory may be /home/user
before = "\\"
after = '/"

which means replacing backslash with / forward slash in ALL files. Now the ONLY place where this replacement takes place is when the LINE contains text "path"

here is what i have , which actually does replace text, but not / , or \


    #!/usr/bin/python
    import os
    import re
    import sys
    
    #print f.readline().replace('\\','/')
    #replace_extensions = []
    replace_extensions = [".tst"]
    
    def try_to_replace(fname):
        if replace_extensions:
            return fname.lower().endswith(replace_extensions)
        return True
    
    
    def file_replace(fname, pat, s_after):
        with open(fname) as f:
            if not any(re.search(pat, line) for line in f):
                return
    
        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 <dir_name> <string_before> <string_after>\n"
        sys.stderr.write(u)
        sys.exit(1)
    
    mass_replace(sys.argv[1], sys.argv[2], sys.argv[3])


and i get error:


    ./mass_replace.py test1 '\\' '/'
    Traceback (most recent call last):
      File "./mass_replace.py", line 43, in <module>
        mass_replace(sys.argv[1], sys.argv[2], sys.argv[3])
      File "./mass_replace.py", line 34, in mass_replace
        if try_to_replace(fname):
      File "./mass_replace.py", line 12, in try_to_replace
        return fname.lower().endswith(replace_extensions)
    TypeError: expected a character buffer object


