title: replace ALL instances of a character with another one in all files hierarchically in directory tree
body: <p>so the command line will look like :</p>
  
  <p>./replace.py   </p>
  
  <p>current directory may be /home/user
  before = "\"
  after = '/"</p>
  
  <p>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"</p>
  
  <p>here is what i have , which actually does replace text, but not / , or \</p>
  
  <pre><code>#!/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 &lt;dir_name&gt; &lt;string_before&gt; &lt;string_after&gt;\n"
      sys.stderr.write(u)
      sys.exit(1)
  
  mass_replace(sys.argv[1], sys.argv[2], sys.argv[3])
  </code></pre>
  
  <p>and i get error:</p>
  
  <pre><code>./mass_replace.py test1 '\\' '/'
  Traceback (most recent call last):
    File "./mass_replace.py", line 43, in &lt;module&gt;
      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
  </code></pre>