#!/usr/bin/env python3 __author__ = 'Anton Vlasenko' import os import sys import re class FilesParser(object): def get_htm_files_list(self, root_dir): "Generate htm files list to process in other function" files_list = [] for root, sub_folders, files in os.walk(root_dir): for file in files: if os.path.splitext(file)[1] == '.htm': files_list.append(os.path.join(root,file)) return files_list def replace(self, source, replacer, root_dir): "Function to replace text in each file given by get_htm_files_list method" counter = 0 # Regexp to compiled pattern. pattern = re.compile(source, re.MULTILINE|re.DOTALL) for file_name in self.get_htm_files_list(root_dir): # Open file to read it. file = open(file_name, "r") # File content -> memory text = file.read() # Close read session. file.close() # Open file to write into it. file = open(file_name, "w") # Replace regexp match with text and write into a file. file.write(pattern.sub(replacer, text)) file.close() # Files counter ++. counter += 1 return counter def find(self, keyword, root_dir): files = list() counter = 0 pattern = re.compile(keyword, re.MULTILINE|re.DOTALL) for file_name in self.get_htm_files_list(root_dir): file = open(file_name, "r") # File content -> memory text = file.read() # Close read session. file.close() if pattern.search(text): files.append(file_name) counter += 1 return files, counter if __name__ == '__main__': # Initializing parser parser = FilesParser() # root dir => current dir (can be read from args, but I don't need this). root_dir = os.getcwd() print ('===========!!!BACKUP YOUR FILES!!!===========') print ('We are going to replace text in all "htm" files in %s directory. Type "s" for search, type "exit" to exit.' % root_dir) regexp = input('What we should replace? (regexp):\n').strip() if regexp == 'exit': sys.exit(0) elif regexp == 's': default_state = False second_phrase = 'Okay, please write here regexp to search for ("exit" to exit):\n' else: default_state = True second_phrase = 'With what we should replace? ("exit" to exit).\n' replacer = input(second_phrase).strip() if replacer == 'exit': sys.exit(0) if default_state: third_phrase = 'Continue? You\'re going to replace %s with %s in all "htm" files in %s and recursive. \ This can\'t be undone ("y" or "n", "yes" or "no"): \n ' % (regexp, replacer, root_dir) else: third_phrase = 'Continue with the search of %s in %s? (("y" or "n", "yes" or "no"): \n' % (replacer, root_dir) okay = input(third_phrase).strip() if okay in ('y', 'yes'): if default_state: counter = parser.replace(regexp, replacer, root_dir) if counter > 0: print ("Done! \n%s files were processed." % str(counter)) else: print ("No files were processed.") else: files_list, counter = parser.find(replacer, root_dir) if files_list: print ('Requested pattern %s was found in %s "htm" files:' % (replacer, counter)) for file in files_list: print (file) else: print ('Sorry, nothing was found') print ('Bye!')