I was using afile.split("/"), but I'm not sure how to impliment it... <br><br>if "/" in afile: (for some reason I can't add 'or "\" in afile' on this line)<br> outfile = open(afile, 'w') # Open output buffer for writing. (to open the file, I can't split here)
<br> outfile.write(zfile.read(afile)) # Write the file. (zfile.read(afile)) wont work if I split here...)<br> outfile.close() # Close the output file buffer.<br><br><div><span class="gmail_quote">
On 10/14/06, <b class="gmail_sendername">Kent Johnson</b> <<a href="mailto:kent37@tds.net">kent37@tds.net</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Chris Hengge wrote:<br>> Ok, last problem with this whole shebang...<br>><br>> When I write the file from the zip, if it is in a subfolder, it will<br>> error..<br>> The code below will detect if the file in contained inside a directory
<br>> in the zip, but I just want it to write it like it wasn't.<br>> Another words<br>><br>> Zipfile.zip looks like this<br>> file.ext<br>> file2.ext<br>> folder/<br>> anotherfile.ext<br>>
<br>> file.ext extracts fine, file2.ext extracts file.. but it see's the last<br>> file as folder/anotherfile.ext and it can't write it.. I tried to figure<br>> out how to use .split to get it working right.. but I'm not having any
<br>> luck.. Thanks.<br>><br>> for afile in zfile.namelist(): # For every file in the zip.<br>> # If the file ends with a needed extension, extract it.<br>> if afile.lower().endswith('.cap') \
<br>> or afile.lower().endswith('.hex') \<br>> or afile.lower().endswith('.fru') \<br>> or afile.lower().endswith('.cfg'):<br>> if afile.__contains__("/"):<br><br>
This should be spelled<br> if "/" in afile:<br><br>__contains__() is the method used by the python runtime to implement<br>'in', generally you don't call double-underscore methods yourself.<br><br>I think you want
<br> afile = afile.rsplit('/', 1)[-1]<br><br>that splits afile on the rightmost '/', if any, and keeps the rightmost<br>piece. You don't need the test for '/' in afile, the split will work<br>correctly whether the '/' is present or not.
<br><br>If you are on Windows you should be prepared for paths containing \ as<br>well as /. You can use re.split() to split on either one.<br><br>Kent<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>> 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><br></blockquote></div><br>