Advertisement
ChrisProsser

replace.py

Jun 27th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from tempfile import mkstemp
  4. from shutil import move
  5. import sys, os, fnmatch
  6.  
  7. # globals
  8. debug_on = True
  9.  
  10. def replace(old_str, new_str, file_path):
  11.     with open(file_path, "r+") as f:
  12.         buf = f.readlines()
  13.         f.seek(0)
  14.         cnt = 0
  15.         new = []
  16.         for line in buf:
  17.             if old_str in line:
  18.                 l = line.replace(old_str, new_str)
  19.                 cnt += 1
  20.             else:
  21.                 l = line
  22.             new.append(l)
  23.         if cnt == 0:
  24.             if debug_on:
  25.                 print "  no matches found in this file"
  26.         else:
  27.             f.truncate()
  28.             for line in new:
  29.                 f.write(line)
  30.             if debug_on:
  31.                 print "  "+str(cnt)+" matches replaced"
  32.         f.close()
  33.  
  34. def get_files(f_str):
  35.     d, ptrn = os.path.split(f_str)
  36.     files = []
  37.     for f in os.listdir(d):
  38.         fx = os.path.split(f)[1]
  39.         if fnmatch.fnmatch(fx, ptrn):
  40.             if '\\' not in f and '/' not in f:
  41.                 new_file = os.path.join(d,f)
  42.             else:
  43.                 new_file = f
  44.             files.append(new_file)
  45.  
  46.     if len(files) == 0:
  47.         print "No files found in this directory matching the pattern:", ptrn
  48.         sys.exit()
  49.  
  50.     return files
  51.  
  52. def main():
  53.     # check cmd line args provided...
  54.     args = len(sys.argv) -1
  55.     if args <> 3:
  56.         print "\nUsage: python replace.py <old_str> <new_str> <file_path|pattern>\n"
  57.         print "The file path will assume the current working directory if none " \
  58.               "is given."
  59.         print "Search is case-sensitive\n"
  60.         print "Example 1 - if a file named example.txt is in your cwd:\n" \
  61.               'python replace.py "old blerb" "new blerb" example.txt\n'
  62.         print "Example 2 - if you wanted to match any .sql files in another directory:\n" \
  63.               'python replace.py "old syntax" "new syntax" "%userprofile%\documents\*.sql"'
  64.         raw_input("\n...press any key to exit")
  65.         sys.exit()
  66.  
  67.     # identify files to be evaluated...
  68.     f_str = sys.argv[3]
  69.     if debug_on:
  70.         print "f_str set to:", f_str
  71.  
  72.     # append path if required
  73.     if '\\' not in f_str and '/' not in f_str:
  74.         f_str = os.path.join(os.getcwd(),f_str)
  75.         if debug_on:
  76.             print "f_str adjusted to:", f_str
  77.  
  78.     # build list of files
  79.     if '*' in f_str:
  80.         files = get_files(f_str)
  81.     else:
  82.         files = [f_str]
  83.  
  84.     # do replacement for each file...
  85.     for f in files:
  86.         if debug_on:
  87.             print "\nAbout to call replace, args:\n  ", sys.argv[1], sys.argv[2], f
  88.         replace(sys.argv[1], sys.argv[2], f)
  89.  
  90. if __name__ == '__main__':
  91.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement