On 10/11/2012 04:48 PM, Matthew Ngaha wrote:
>>
>> 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?

They aren't arguments to anything.  But sys.argv would be undefined if
you had not imported sys.

>
> 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)

When you read this, what does it tell you?  Decompose the statement as I
showed you, and see what the args actually are by printing them. 
Clearly, the error message tells you that you have a list of size 0
instead of size 3.  (The self argument is implied, since you're creating
an instance)

I suggest you look up sys.argv to see what these arguments actually
mean.  If you don't supply any of them, then you'll get this error.

Are you learning from a tutorial?  Does it have argv in its index?  Have
you tried googling for    python sys.argv ?

When you run this script, what arguments DO you type in ?

python myscript.py  arg1  arg2   arg3

>
> 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] ?
>

Those first three are imports, not arguments to anything.  And the
second 3 are 3 of the formal parameters to the __init__() method.  The
arguments come from the slice, which comes from sys.argv, which comes
from the command line.

You never showed us how you run the program, so how do we know what the
cmdline arguments are?


-- 

DaveA

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to