Browse Source

added zipreplace_comp.py as composition example

master
T. Meissner 10 years ago
parent
commit
4597685ec8
1 changed files with 25 additions and 0 deletions
  1. +25
    -0
      python_3_oop/chapter05/zipreplace_comp.py

+ 25
- 0
python_3_oop/chapter05/zipreplace_comp.py View File

@ -0,0 +1,25 @@
from zipprocessor import ZipProcessorComp
import sys
import os
class ZipReplace:
def __init__(self, search_string, replace_string):
self.search_string = search_string
self.replace_string = replace_string
def process(self, zipprocessor):
"perform a search and replace on all files in the temporary directory"
for filename in os.listdir(zipprocessor.temp_directory):
with open(zipprocessor._full_filename(filename)) as file:
contents = file.read()
contents = contents.replace(self.search_string,
self.replace_string)
with open(zipprocessor._full_filename(filename), "w") as file:
file.write(contents)
if __name__ == "__main__":
zipreplace = ZipReplace(*sys.argv[2:4])
ZipProcessorComp(sys.argv[1], zipreplace).process_zip()

Loading…
Cancel
Save