[Tutor] Zipfile and File manipulation questions.

Chris Hengge pyro9219 at gmail.com
Sun Oct 15 02:39:57 CEST 2006


Ok, last problem with this whole shebang...

When I write the file from the zip, if it is in a subfolder, it will error..

The code below will detect if the file in contained inside a directory in
the zip, but I just want it to write it like it wasn't.
Another words

Zipfile.zip looks like this
file.ext
file2.ext
folder/
        anotherfile.ext

file.ext extracts fine, file2.ext extracts file.. but it see's the last file
as folder/anotherfile.ext and it can't write it.. I tried to figure out how
to use .split to get it working right.. but I'm not having any luck..
Thanks.

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('.cfg'):
            if afile.__contains__("/"):
                outfile = open(afile, '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.

On 10/14/06, Chris Hengge <pyro9219 at gmail.com> wrote:
>
> Got that working now, thanks.
>
> I've been using activepythons release, and they dont have 2.5 prepared
> yet. Guess I should just upgrade without them?
>
> On 10/14/06, Kent Johnson <kent37 at tds.net> wrote:
> >
> > Chris Hengge wrote:
> > > Oops.... I get an error using that code..
> > >
> > >     if filename.endswith('.cap','.fru','.hex') or
> > > filename.endswith('.sdr', '.cfg'):
> > > TypeError: slice indices must be integers or None
> >
> > With Python 2.5 you can do this with a tuple argument. You need an extra
> > set of parentheses to create the tuple:
> > if filename.endswith(('.cap','.fru','.hex', '.sdr', '.cfg')):
> >
> > In Python 2.4 or less you need a separate endswith() for each ending.
> >
> > Kent
> >
> > _______________________________________________
> > 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/20061014/02b05e8b/attachment.htm 


More information about the Tutor mailing list