[Tutor] Unzipping a Zip of folders that have zips within them that I'd like to unzip all at once.
Oscar Benjamin
oscar.j.benjamin at gmail.com
Mon Sep 24 04:17:43 CEST 2012
On 24 September 2012 03:00, Gregory Lund <gnj091405 at gmail.com> wrote:
> >>> > You're trying to open the directory as if it were a zipfile. You
> should
> >>> > pass in the path to a zipfile not to a folder.
> >>>
> >>> Agreed, but how do I do that "pass in the path to a zipfile'?
> >>> Using an if statement I presume?
> >>>
> >>> > >
> >>>
> >>> > > I guess my first issue is to resolve the 'Permission denied'
> problem.
> >>> > > And, I know I need an 'if' statement, somehow...
> >>> >
> >>> Yes, the permission denied problem seems to be the first real
> >>> obstacle, but I can't figure it out.
> >>> I tried searching online but everything I found to try, didn't work.
> >>
> >>
> >> The permission error is because you are trying to open a folder as if it
> >> were a file. The operating system does not give you permission to do
> this
> >> because it is a nonsensical operation.
> >>
> >>> > Remove the folder paths from the list of paths.
> >>>
> >>> How can I remove the folder paths from the list of paths?
> >>
> >>
> >> You can do it with a list comprehension:
> >>
> >> import os.path
> >> paths = [p for p in paths if os.path.isdir(p)]
> >
> >
> > Sorry that should be (note the 'not'):
> > paths = [p for p in paths if not os.path.isdir(p)]
> >
> Ok, but where do I put that?
> Sorry Oscar, I don't know where to put it, nor do I know how to use
> 'paths' once I do that.
>
> I was trying this prior to getting your email about paths...(I put
> this after z.close()
>
> for item in zipContents:
> if item(-4) == ('.zip'):
>
That should be:
if item[-4:] == '.zip'
but a better way to do that is:
if item.endswith('.zip')
x = zipfile.Zipfile(item,'r')
> x.extractall()
> x.close()
>
> but, I kept getting a "TypeError: 'str' object is not callable" error.
>
> then I tried
>
> for item in zipContents:
> if item.lower() .endswith(".zip"):
>
Ok that's better.
> item.extractall()
> item.close()
>
> but got a " item.extractall()
> AttributeError: 'str' object has no attribute 'extractall'" error
>
item is a string object. You need to use that string to create a ZipFile
object and then call extractall() on the ZipFile object (the way you did in
your original post in this thread).
Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120924/9a6409fc/attachment.html>
More information about the Tutor
mailing list