Advertisement
redsees

Untitled

Oct 7th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 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.         if item.find('py') != -1:
  17.             continue
  18.         old_value = sys.argv[1].lower()
  19.         new_value = sys.argv[2]
  20.        
  21.         #Getting a backup of the file
  22.         shutil.copyfile(os.path.join(subdirs,item),os.path.join(subdirs,item)+'.backup')
  23.  
  24.         #Checking filename if it contains old value
  25.         item_name = os.path.join(subdirs,item).lower()
  26.        
  27.         fp = open(os.path.join(subdirs,item),'r+')
  28.         file_contents = fp.read().lower()
  29.         new_file_contents = file_contents.replace(old_value, new_value)
  30.            
  31.         if item_name.find(old_value) == -1:
  32.             fp.seek(0,0)
  33.             fp.write(new_file_contents)
  34.         else:
  35.             new_fp = open(item_name.replace(old_value, new_value),'wr')
  36.             new_fp.write(new_file_contents)
  37.             new_fp.close()
  38.             os.remove(os.path.join(subdirs,item))
  39.         fp.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement