Advertisement
redsees

Untitled

Oct 6th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. try:
  3.     import os,shutil,sys
  4. except ImportError:
  5.     print "[!] One or more library is missing...\n\n"
  6.     exit(-1)
  7.  
  8. current_dir = '.'
  9.  
  10. if len(sys.argv) != 3:
  11.     print 'Usage:\n\n%s <old_value> <new_value>\n\n' % (sys.argv[0])
  12.     exit(-1)
  13.  
  14. for subdirs, dirs, files in os.walk(current_dir):
  15.     for item in files:
  16.         old_value = sys.argv[1].lower()
  17.         new_value = sys.argv[2]
  18.        
  19.         #Getting a backup of the file
  20.         shutil.copyfile(os.path.join(subdirs,item),os.path.join(subdirs,item)+'.backup')
  21.  
  22.         #Checking filename if it contains old value
  23.         item_name = os.path.join(subdirs,item).lower()
  24.        
  25.         fp = open(item,'r')
  26.         file_contents = fp.read()
  27.         new_file_contents = file_contents.replace(old_value, new_value)
  28.            
  29.         if item_name.find(old_value) == -1:
  30.             fp.write(new_file_contents)
  31.         else:
  32.             new_fp = open(item_name.replace(old_value, new_value),'w')
  33.             new_fp.write(new_file_contents)
  34.             new_fp.close()
  35.         fp.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement