class ZipReplace: def __init__(self, filename, search_string, replace_string): """ Init class (str, str, str)->None Need path to zip file, string to search and string to replace """ self.processor = ZipProcessor(filename) self.search_string = search_string self.replace_string = replace_string self.processor.process_zip(self) def process_files(self): """ function, which replace content """ for filename in os.listdir(self.processor.temp_directory): with open(self.processor._full_filename( self.processor.zipname)) as file: contents = file.read() contents = contents.replace(self.search_string, self.replace_string) with open(self.processor._full_filename(self.processor.zipname), "w") as file: file.write(contents)
class ScaleZip: def __init__(self, filename): """ Init class (str)->None Need path to zip file """ self.processor = ZipProcessor(filename) self.processor.process_zip(self) def process_files(self): '''Scale each image in the directory to 640x480''' for filename in os.listdir(self.processor.temp_directory): try: im = image.load(self.processor._full_filename(filename)) scaled = scale(im, (640,480)) image.save(scaled, self.processor._full_filename(filename)) except: pass
class ZipReplace: def __init__(self, filename, search_string, replace_string): self.ex = ZipProcessor(filename) self.filename = filename self.temp_directory = str(filename[:-4]) #super().__init__(filename) self.search_string = search_string self.replace_string = replace_string def process_files(self): '''perform a search and replace strings on all files in the temporary directory''' for filename in os.listdir(self.temp_directory): with open( self.ex._full_filename(filename).replace('unzipped-', '')) as file: contents = file.read() contents = contents.replace(self.search_string, self.replace_string) with open( self.ex._full_filename(filename).replace('unzipped-', ''), "w") as file: file.write(contents)