Extracting file from zip archive in Python 2.6.1

B

Brandon Taylor

Hello everyone,

I'm having an issue specifying the path for extracting files from
a .zip archive. In my method, I have:

zip_file.extract(zip_name + '/' + thumbnail_image, thumbnail_path)

What is happening is that the extract method is creating a folder with
the name of 'zip_name' and extracting the files to it. Example:

if 'thumbnail_path' is 'images/inventory/thumbnails/'

extract is creating: 'images/inventory/thumbnails/test1/'

and saving the files out. How can I set the path to be exactly:
'images/inventory/thumbnails' ?

TIA,
Brandon
 
G

Gabriel Genellina

En Tue, 03 Feb 2009 05:31:24 -0200, Brandon Taylor
I'm having an issue specifying the path for extracting files from
a .zip archive. In my method, I have:

zip_file.extract(zip_name + '/' + thumbnail_image, thumbnail_path)

What is happening is that the extract method is creating a folder with
the name of 'zip_name' and extracting the files to it. Example:

extract will create all directories in member name. Use open instead:

with zip_file.open(zip_name + '/' + thumbnail_image) as source:
with open(os.path.join(thumbnail_path, thumbnail_image), "wb") as target:
shutil.copyfileobj(source, target)

(untested)
 
B

Brandon Taylor

En Tue, 03 Feb 2009 05:31:24 -0200, Brandon Taylor  




extract will create all directories in member name. Use open instead:

with zip_file.open(zip_name + '/' + thumbnail_image) as source:
   with open(os.path.join(thumbnail_path, thumbnail_image), "wb") as target:
     shutil.copyfileobj(source, target)

(untested)


Hi Gabriel,

Thank you for the code sample. I figured I was going to have to use
'open', but I completely forgot about the 'with' statement. I was
trying to figure out how to get access to the file object in the zip
without having to loop over all of the items, and 'with' will allow me
to do just that.

I'll give it a shot when I get home this evening and post my results.

Kind regards,
Brandon
 
B

Brandon Taylor

Hi Gabriel,

Thank you for the code sample. I figured I was going to have to use
'open', but I completely forgot about the 'with' statement. I was
trying to figure out how to get access to the file object in the zip
without having to loop over all of the items, and 'with' will allow me
to do just that.

I'll give it a shot when I get home this evening and post my results.

Kind regards,
Brandon

Ok, the first thing I needed to do was add:

from __future__ import with_statement at the beginning of my file

but:

with zip_file.open(zip_name + '/' + thumbnail_image) as source:
with open(os.path.join(thumbnail_path,
thumbnail_image), 'wb') as target:
shutil.copyfileobj(source, target)

Returns an error on the first line:

ZipExtFile instance has no attribute '__exit__'

Googling this error message is turning up nothing, and there's no
mention of the exception in the docs. Any thoughts?

TIA,
Brandon
 
M

MRAB

Brandon said:
>
> Ok, the first thing I needed to do was add:
>
> from __future__ import with_statement at the beginning of my file
>
> but:
>
> with zip_file.open(zip_name + '/' + thumbnail_image) as source:
> with open(os.path.join(thumbnail_path,
> thumbnail_image), 'wb') as target:
> shutil.copyfileobj(source, target)
>
> Returns an error on the first line:
>
> ZipExtFile instance has no attribute '__exit__'
>
> Googling this error message is turning up nothing, and there's no
> mention of the exception in the docs. Any thoughts?
>
The 'with' statement relies on the object having __enter__ and __exit__
methods. It looks like the object returned by zip_file.open() doesn't
have them.
 
R

rdmurray

Quoth Brandon Taylor said:
Ok, the first thing I needed to do was add:

from __future__ import with_statement at the beginning of my file

but:

with zip_file.open(zip_name + '/' + thumbnail_image) as source:
with open(os.path.join(thumbnail_path,
thumbnail_image), 'wb') as target:
shutil.copyfileobj(source, target)

Returns an error on the first line:

ZipExtFile instance has no attribute '__exit__'

Googling this error message is turning up nothing, and there's no
mention of the exception in the docs. Any thoughts?

Yeah, that means the ZipExtFile object hasn't been extended to
handle the context management protocol. It would be pretty
simple to roll your own for it. Take a look at the contextlib
module.

--RDM
 
B

Brandon Taylor

Quoth Brandon Taylor <[email protected]>:








Yeah, that means the ZipExtFile object hasn't been extended to
handle the context management protocol.  It would be pretty
simple to roll your own for it.  Take a look at the contextlib
module.

--RDM

Cool. Thanks for the pointers. In the meantime, I just used extract
and os.rename to move the files where I need them, but I'll see if I
can save that extra step if possible.
 
G

Gabriel Genellina

En Wed, 04 Feb 2009 00:36:40 -0200, Brandon Taylor
Ok, the first thing I needed to do was add:

from __future__ import with_statement at the beginning of my file

That should not be necesary with your Python version (2.6.1 isn't it?)
with zip_file.open(zip_name + '/' + thumbnail_image) as source:
with open(os.path.join(thumbnail_path,
thumbnail_image), 'wb') as target:
shutil.copyfileobj(source, target)

Returns an error on the first line:

ZipExtFile instance has no attribute '__exit__'

Ouch, sorry, this new feature will appear in the not-yet-released 2.7
version...
Try this instead:

source = zip_file.open(zip_name + '/' + thumbnail_image)
try:
with open(os.path.join(thumbnail_path, thumbnail_image), 'wb') as target:
shutil.copyfileobj(source, target)
finally:
source.close()
 
B

Brandon Taylor

En Wed, 04 Feb 2009 00:36:40 -0200, Brandon Taylor  
<[email protected]> escribió:





That should not be necesary with your Python version (2.6.1 isn't it?)




Ouch, sorry, this new feature will appear in the not-yet-released 2.7  
version...
Try this instead:

source = zip_file.open(zip_name + '/' + thumbnail_image)
try:
   with open(os.path.join(thumbnail_path, thumbnail_image), 'wb') as target:
     shutil.copyfileobj(source, target)
finally:
   source.close()

Awesome. Works perfectly, and saves me the extra step of having to
move the files.

Many, many thanks!

Kind regards,
Brandon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top