Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:18:22+00:00 2026-06-18T07:18:22+00:00

How can I unzip a .zip file with Python into some directory output_dir and

  • 0

How can I unzip a .zip file with Python into some directory output_dir and fetch a list of all the directories made by the unzipping as a result? For example, if I have:

unzip('myzip.zip', 'outdir')

outdir is a directory that might have other files/directories in it. When I unzip myzip.zip into it, I’d like unzip to return all the directories made in outdir/ as a result of the zipping. Here is my code so far:

import zipfile
def unzip(zip_file, outdir):
    """
    Unzip a given 'zip_file' into the output directory 'outdir'.
    """
    zf = zipfile.ZipFile(zip_file, "r")
    zf.extractall(outdir)

How can I make unzip return the dirs it creates in outdir? thanks.

Edit: the solution that makes most sense to me is to get ONLY the top-level directories in the zip file and then recursively walk through them which will guarantee that I get all the files made by the zip. Is this possible? The system specific behavior of namelist makes it virtually impossible to rely on

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:18:24+00:00Added an answer on June 18, 2026 at 7:18 am

    You can read the contents of the zip file with the namelist() method. Directories will have a trailing path separator:

    >>> import zipfile
    >>> zip = zipfile.ZipFile('test.zip')
    >>> zip.namelist()
    ['dir2/', 'file1']
    

    You can do this before or after extracting contents.

    Depending on your operating environment, the result of namelist() may be limited to the top-level paths of the zip archive (e.g. Python on Linux) or may cover the full contents of the archive (e.g. IronPython on Windows).

    The namelist() returns a complete listing of the zip archive contents, with directories marked with a trailing path separator. For instance, a zip archive of the following file structure:

    ./file1
    ./dir2
    ./dir2/dir21
    ./dir3
    ./dir3/file3
    ./dir3/dir31
    ./dir3/dir31/file31
    

    results in the following list being returned by zipfile.ZipFile.namelist():

    [ 'file1', 
      'dir2/', 
      'dir2/dir21/', 
      'dir3/', 
      'dir3/file3', 
      'dir3/dir31/', 
      'dir3/dir31/file31' ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to unzip some zip file, it has about 65 megs. Code
I searched for unzipping the .zip file which is downloaded in the document directory.
Can someone help me how to unzip a zip file in VB.Net ? im
I'm using VCLZip 2.21 to unzip a zip file. I can make TVCLUnzip overwrite
How can I unzip a file in MacOS X Obj-C? What frameworks do I
I have one zip file that i want unzip .the size of this file
A compressed file can be classified into below logical groups a. The operating system
I use python's zipfile module to extract a .zip archive (Let's take this file
Is there a library in boost that can be used to unzip a zip
I'm downloading a zip file with phoneGap and i have to unzip it. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.