FAQ
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

Search Discussions

  • Gabriel Genellina at Feb 3, 2009 at 3:45 pm
    En Tue, 03 Feb 2009 05:31:24 -0200, Brandon Taylor
    <btaylordesign at gmail.com> escribi?:
    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)

    --
    Gabriel Genellina
  • Brandon Taylor at Feb 3, 2009 at 7:16 pm

    On Feb 3, 9:45?am, "Gabriel Genellina" wrote:
    En Tue, 03 Feb 2009 05:31:24 -0200, Brandon Taylor ?
    <btaylordes... at gmail.com> escribi?:
    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)

    --
    Gabriel Genellina

    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
  • Brandon Taylor at Feb 4, 2009 at 2:36 am

    On Feb 3, 1:16?pm, Brandon Taylor wrote:
    On Feb 3, 9:45?am, "Gabriel Genellina" wrote:


    En Tue, 03 Feb 2009 05:31:24 -0200, Brandon Taylor ?
    <btaylordes... at gmail.com> escribi?:
    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)
    --
    Gabriel Genellina
    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
  • MRAB at Feb 4, 2009 at 2:53 am

    Brandon Taylor wrote:
    On Feb 3, 1:16 pm, Brandon Taylor wrote:
    On Feb 3, 9:45 am, "Gabriel Genellina" wrote:
    >>
    >>
    >>
    En Tue, 03 Feb 2009 05:31:24 -0200, Brandon Taylor
    <btaylordes... at gmail.com> escribi?:
    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)
    --
    Gabriel Genellina
    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?
    >
    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.
  • Rdmurray at Feb 4, 2009 at 3:15 am

    Quoth Brandon Taylor <btaylordesign at gmail.com>:
    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
  • Brandon Taylor at Feb 4, 2009 at 3:56 am

    On Feb 3, 9:15?pm, rdmur... at bitdance.com wrote:
    Quoth Brandon Taylor <btaylordes... at gmail.com>:


    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
    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.
  • Gabriel Genellina at Feb 4, 2009 at 6:16 am
    En Wed, 04 Feb 2009 00:36:40 -0200, Brandon Taylor
    <btaylordesign at gmail.com> escribi?:
    On Feb 3, 1:16?pm, Brandon Taylor wrote:
    On Feb 3, 9:45?am, "Gabriel Genellina" wrote:
    En Tue, 03 Feb 2009 05:31:24 -0200, Brandon Taylor ?
    <btaylordes... at gmail.com> escribi?:
    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)
    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()

    --
    Gabriel Genellina
  • Brandon Taylor at Feb 5, 2009 at 9:47 pm

    On Feb 4, 12:16?am, "Gabriel Genellina" wrote:
    En Wed, 04 Feb 2009 00:36:40 -0200, Brandon Taylor ?
    <btaylordes... at gmail.com> escribi?:


    On Feb 3, 1:16?pm, Brandon Taylor wrote:
    On Feb 3, 9:45?am, "Gabriel Genellina" wrote:
    En Tue, 03 Feb 2009 05:31:24 -0200, Brandon Taylor ?
    <btaylordes... at gmail.com> escribi?:
    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)
    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()

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

    Many, many thanks!

    Kind regards,
    Brandon

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 3, '09 at 7:31a
activeFeb 5, '09 at 9:47p
posts9
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase