Advertisement
moytrage

str_repl.py

Jun 24th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. # Replaces all strings in all files with given encoding and extension recursively in all sub directories.
  2. # Strings to replace are given by .def file, every line should look like before_string:after_string
  3.  
  4. import os
  5.  
  6. subs_file = 'subs.def'
  7. encoding = 'utf-8'
  8. extension = '.vcxproj'
  9.  
  10. subs = {}
  11.  
  12. f = open(subs_file)
  13. for line in f.readlines():
  14.   (before, after) = line.split(':')
  15.   after = after.rstrip()
  16.   subs[before] = after
  17.  
  18. for root, dirs, files in os.walk('.'):
  19.   for fname in files:
  20.     if not fname.endswith(extension): continue
  21.     path = os.path.join(root, fname)
  22.     print("Processing " + path)
  23.     f = open(path, "rb")
  24.     content_str = f.read().decode(encoding)
  25.     f.close()
  26.     for before in subs: content_str = content_str.replace(before, subs[before])
  27.     f = open(path, "wb")
  28.     f.write(content_str.encode(encoding))
  29.     f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement