[Tutor] Zipfile and File manipulation questions.
Chris Hengge
pyro9219 at gmail.com
Mon Oct 16 18:21:46 CEST 2006
Excellent suggestions! I'll see how many of them I can impliment.
Part of my project 'criteria' is that someone that doesn't know python (or
possibly programming) should be able to follow my work to add a new system
to the list. Right now I've got it setup so it only takes 3 steps. The
reason for this is that I've inherited perl scripts that supposedly can do
parts of what my project can do, except I dont know perl, and I dont want to
either.. The language (atleast in these scripts) is VERY cryptic... no
commenting, no implimentation for adding new output(which I have). This
position is short term that high school interns tend to have, so I'm not
going to be doing this very long, but it provided a great chance to work
with a real world project for python, so I took it.
As for python 2.5, I've not made the jump yet... I was waiting for active
python to make their release... Not sure why, but this(active python) is
what I'm used to =D. That, and I'm not sure how SPE will handle python
2.5and from the feedback I've gotten from the author, he's not able to
maintin
his project at the moment, which is a shame.
If I make changes based on your suggestions, I'll re-share the updated code
incase anyone out there wants to see.
On 10/16/06, Kent Johnson <kent37 at tds.net> wrote:
>
> 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"
> >
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061016/a0be9b83/attachment.html
More information about the Tutor
mailing list