[Tutor] Zipfile and File manipulation questions.

Chris Hengge pyro9219 at gmail.com
Mon Oct 16 20:32:30 CEST 2006


Using one of the things you suggested...
>                 if "/" in afile:
>                     aZipFile = afile.rsplit('/', 1)[-1] # Split file
> based on criteria.
>                     outfile = open(aZipFile, 'w') # Open output buffer
> for writing.
>                     outfile.write(zfile.read(afile)) # Write the file.
>                     outfile.close() # Close the output file buffer.
>                 elif "\\" in afile:
>                     aZipFile = afile.rsplit('\\', 1)[-1] # Split file
> based on criteria.
>                     outfile = open(aZipFile, 'w') # Open output buffer
> for writing.
>                     outfile.write(zfile.read(afile)) # Write the file.
>                     outfile.close() # Close the output file
> buffer.
>                 else:
>                     outfile = open(afile, 'w') # Open output buffer for
> writing.
>                     outfile.write(zfile.read(afile)) # Write the file.
>                     outfile.close() # Close the output file buffer.

Here you have three copies of the code to process the file. One way to
avoid duplication is to factor duplicated code into a separate function
but in this case I would preprocess afile, then do the work:

if "/" in afile:
   afile = afile.rsplit('/', 1)[-1]
elif  "\\" in afile:
   afile = afile.rsplit('\\', 1)[-1]

##############################################################

How do I account for the seperation I had? If you look closely...

>                     outfile = open(afile, 'w') # Open output buffer for
> writing.
>                     outfile.write(zfile.read(afile)) # Write the file.
>                     outfile.close() # Close the output file buffer.

is not the same as:

>                     aZipFile = afile.rsplit('\\', 1)[-1] # Split file
> based on criteria.
>                     outfile = open(aZipFile, 'w') # Open output buffer
> for writing.
>                     outfile.write(zfile.read(afile)) # Write the file.
>                     outfile.close() # Close the output file
> buffer.

The problem is, outfile.write(zfile.read(whatever)) 'whatever' must have the
slashes in order to find the file I want inside the zip to extract..

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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061016/78b1f876/attachment.html 


More information about the Tutor mailing list