[Tutor] Zipfile and File manipulation questions.

Kent Johnson kent37 at tds.net
Mon Oct 16 12:07:37 CEST 2006


Chris Hengge wrote:
> I like this little snip of code from your suggestion, and I may 
> incorporate it...
> 
> for ext in ['.cap', '.hex', '.fru', '.cfg']:
>         if aFile.lower().endswith(ext):
>             return True
> 
> Just for sake of sharing.. here is my entire method..

Hopefully for the sake of feedback also...

You have a lot of duplicated code that is pretty easy to remove, and a 
few places where there are easier ways to do it. In general duplicated 
code is a bad thing - it makes your program larger and harder maintain.

Copy and paste is the enemy of readable, maintainable code.

> 
> 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:
>         if filePathName[0] == '"': # If the path includes quotes, remove 
> them.
>             zfile = zipfile.ZipFile(filePathName[1:-1], "r")
>         else: # If the path doesn't include quotes, dont change.
>             zfile = zipfile.ZipFile(filePathName, "r")

I would write:
filePathName = filePathName.strip('"')
zfile = zipfile.ZipFile(filePathName, "r")

>         for afile in zfile.namelist(): # For every file in the zip.
>             # If the file ends with a needed extension, extract it.
>             if afile.lower().endswith('.cap') \
>             or afile.lower().endswith('.hex') \
>             or afile.lower().endswith('.fru') \
>             or afile.lower().endswith('.sdr') \
>             or afile.lower().endswith('.cfg'):

The suggestion to use a loop here is excellent. If you are using Python 
2.5 it's even easier:
if afile.lower().endswith( ('.cap', '.hex', '.fru', '.cfg') ):

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

You could do both of the above in one split using re.split() but I'm not 
sure you are ready for that...

Now you can just use afile, it is the filename you want and you need 
only one copy of the following code:

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

Kent


>         print "Resource extraction completed successfully!\n"
> 



More information about the Tutor mailing list