[Tutor] Zipfile and File manipulation questions.

Chris Hengge pyro9219 at gmail.com
Mon Oct 16 21:26:12 CEST 2006


I chose the way I used the names because to me...

outFile = open(aFile.lower(), 'w') # Open output buffer for writing.
= open a file with lowercase name for writing.
it is implied that aFile is from the zip, since it is created in the loop to
read the zip..

outFile.write(zFile.read(insideZip)) # Write the file.
= write what is read from inside the zip file.

I guess for declaration it isn't very clear, but thats what comments are
for?
My naming was purely for my ease of mind.. I personally care less about what
I call it when I declare, as to how it logically flows when I go to use it.
I'm sure this is considered poor method, but once I declare a method I tend
to never need to change the declaration, just how I use the info... I hope
that makes sense.

On 10/16/06, Kent Johnson <kent37 at tds.net> wrote:
>
> Chris Hengge wrote:
> > Here is my solution, completed with (I think) all your suggestions...
> >
> >
> #########################################################################
> > def extractZip(filePathName):
> >     """
> >     This method recieves the zip file name for decompression, placing
> the
> >     contents of the zip file appropriately.
> >     """
> >     if filePathName == "":
> >         print "No file provided...\n"
> >     else:
> >         try: # Attempt to unzip file.
> >             zFile = zipfile.ZipFile(filePathName.strip('"'), "r")
> >             for aFile in zFile.namelist(): # For every file in the zip.
> >                 # If the file ends with a needed extension, extract it.
> >                 for ext in ['.cap', '.hex', '.fru', '.cfg', '.sdr']:
> >                     if aFile.lower().endswith(ext):
> >                         insideZip = aFile # Copy of Filename.
> >                         if "/" in aFile: # Split the filename if '/'.
> >                           aFile = aFile.rsplit('/', 1)[-1]
> >                         elif  "\\" in aFile: # Split the filename if
> '\'.
> >                           aFile = aFile.rsplit('\\',
> > 1)[-1]
> >                         outfile = open( aFile.lower(), 'w') # Open
> > output buffer for writing.
> >                         outfile.write(zFile.read(insideZip)) # Write the
> > file.
> >                         outfile.close() # Close the output file buffer.
> >             print "Resource extraction completed successfully!\n"
> >         except IOerror, message: # If file creation fails, let the user
> > know.
> >             print "File could not be written: \n"
> >             print message
> >
> >
> #########################################################################
> > Definatly an improvement! Thanks Kent.
>
> Yes, that is what I meant. One minor quibble, I think I would keep aFile
>   as the name in the zip, since that is what it starts as, and use a new
> name for the external file name. Maybe you could use better names, for
> example zipPath and fileName. I think that would make the code a little
> clearer but it is a very minor point.
>
> Kent
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061016/123b5222/attachment.html 


More information about the Tutor mailing list