Using one of the things you suggested...<br>> if "/" in afile:<br>> aZipFile = afile.rsplit('/', 1)[-1] # Split file<br>> based on criteria.<br>> outfile = open(aZipFile, 'w') # Open output buffer
<br>> for writing.<br>> outfile.write(zfile.read(afile)) # Write the file.<br>> outfile.close() # Close the output file buffer.<br>> elif "\\" in afile:
<br>> aZipFile = afile.rsplit('\\', 1)[-1] # Split file<br>> based on criteria.<br>> outfile = open(aZipFile, 'w') # Open output buffer<br>> for writing.<br>>
outfile.write(zfile.read(afile)) # Write the file.<br>> outfile.close() # Close the output file<br>> buffer.<br>> else:<br>> outfile = open(afile, 'w') # Open output buffer for
<br>> writing.<br>> outfile.write(zfile.read(afile)) # Write the file.<br>> outfile.close() # Close the output file buffer.<br><br>Here you have three copies of the code to process the file. One way to
<br>avoid duplication is to factor duplicated code into a separate function<br>but in this case I would preprocess afile, then do the work:<br><br>if "/" in afile:<br> afile = afile.rsplit('/', 1)[-1]<br>elif "\\" in afile:
<br> afile = afile.rsplit('\\', 1)[-1]<br><br>##############################################################<br><br>How do I account for the seperation I had? If you look closely... <br><br>> outfile = open(afile, 'w') # Open output buffer for
<br>
> writing.<br>
> outfile.write(zfile.read(afile)) # Write the file.<br>
> outfile.close() # Close the output file buffer.<br><br>is not the same as:<br><br>> aZipFile = afile.rsplit('\\', 1)[-1] # Split file<br>
> based on criteria.<br>
> outfile = open(aZipFile, 'w') # Open output buffer<br>
> for writing.<br>
> outfile.write(zfile.read(afile)) # Write the file.<br>
> outfile.close() # Close the output file<br>
> buffer.<br><br>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.. <br><br>Here is my solution, completed with (I think) all your suggestions...
<br><br>#########################################################################<br>def extractZip(filePathName):<br> """<br> This method recieves the zip file name for decompression, placing the<br>
contents of the zip file appropriately.<br> """<br> if filePathName == "":<br> print "No file provided...\n"<br> else:<br> try: # Attempt to unzip file.<br> zFile =
zipfile.ZipFile(filePathName.strip('"'), "r")<br> for aFile in zFile.namelist(): # For every file in the zip.<br> # If the file ends with a needed extension, extract it. <br> for ext in ['.cap', '.hex', '.fru', '.cfg', '.sdr']:
<br> if aFile.lower().endswith(ext):<br> insideZip = aFile # Copy of Filename. <br> if "/" in aFile: # Split the filename if '/'.<br> aFile =
aFile.rsplit('/', 1)[-1]<br> elif "\\" in aFile: # Split the filename if '\'.<br> aFile = aFile.rsplit('\\', 1)[-1] <br> outfile = open(
aFile.lower(), 'w') # Open output buffer for writing.<br> outfile.write(zFile.read(insideZip)) # Write the file.<br> outfile.close() # Close the output file buffer.<br> print "Resource extraction completed successfully!\n"
<br> except IOerror, message: # If file creation fails, let the user know.<br> print "File could not be written: \n"<br> print message<br><br>#########################################################################
<br>Definatly an improvement! Thanks Kent.<br>