[Tutor] need an explanation

Matthew Ngaha chigga101 at gmail.com
Thu Oct 11 22:48:40 CEST 2012


>
>
> Obviously a Monty Python fan as I see 3 methods :)
>
lol i dont know what i was looking at.. yes its 3 methods sorry:(


>
>> def __init__(self):
>>      self.zipping_directory = "unzipped-{}".format(filename)
>>
>
> Where did filename appear from above?
>
>
>
sorry i didnt write everything. the init method also had...
self.filename = filename  ... i will paste the whole code


>
> I suggest that you show us some real code that will run with some print
> statements in appropriate places to show us what is happening.  That way
> you may well be able to answer your own questions and learn at the same
> time.
>
>>
>
> sadly when i run the file i get an error so i dont know what to doto fix
it and be able to run

@ DAVE.. you said
sys is a module (presumably you have an import
somewhere above this line). In the module, there's a list argv.

the import statements are:

import sys
import os
import shutil
import zipfile

so im guessing [sys, os, shutil, zipfile]  these are the arguments being
passed? my mind tells me no, as these are more likely the arguments in the
A_Class init method?

here is he full code... i changed the names in the mail to make it clearer.
so the names in the code will be different. A_Class is actually ZipReplace
etc..

i cant test it because on start the program returns this error and i dont
know how to fix it:

    ZipReplace(*sys.argv[1:4]).zip_find_replace()
TypeError: __init__() takes exactly 4 positional arguments (1 given)


full code:

import sys
import os
import shutil
import zipfile

class ZipReplace:
    def __init__(self, filename, search_string, replace_string):
        self.filename = filename
        self.search_string = search_string
        self.replace_string = replace_string
        self.temp_directory = "unzipped-{}".format(
                filename)

   def _full_filename(self, filename):
        return os.path.join(self.temp_directory, filename)

    def zip_find_replace(self):
        self.unzip_files()
        self.find_replace()
        self.zip_files()

   def unzip_files(self):
        os.mkdir(self.temp_directory)
        zip = zipfile.ZipFile(self.filename)
        try:
            zip.extractall(self.temp_directory)
        finally:
            zip.close()

    def find_replace(self):
        for filename in os.listdir(self.temp_directory):
            with open(self._full_filename(filename)) as file:
                contents = file.read()
            contents = contents.replace(
                    self.search_string, self.replace_string)
            with open(self._full_filename(filename), "w") as file:
                file.write(contents)

   def zip_files(self):
        file = zipfile.ZipFile(self.filename, 'w')
        for filename in os.listdir(self.temp_directory):
            file.write(self._full_filename(filename), filename)
        shutil.rmtree(self.temp_directory)

if __name__ == "__main__":
    ZipReplace(*sys.argv[1:4]).zip_find_replace()

is a bit too advanced for me but i now see what it does.. although i wish
it didnt return an error when run.

so the arguments being passed are...

[os, shutil, zipfile] or [filename, search_string, return_string] ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121011/aac540ce/attachment.html>


More information about the Tutor mailing list