0

I'm looking for a (simple) way to extract a zip folder, but ignore top folders, or a certain path when extracting.

An example:

   - topdir/subdir/<files and dirs I want>

So I don't want to strip absolutely all directories away, but the top dirs "topdir" and "subdir". I currently have this code:

def extract_zip(zip_file, extraction_path):
    with ZipFile(zip_file, 'r') as archive:
        try: 
            for file in archive.namelist():
                if file.startswith('topdir/subdir/'):
                    archive.extract(file, extraction_path)

The content from "topdir" is now ignored, and I get the content from "subdir" extracted. But the topdir and subdir is still created, with my content inside "subdir".

How can I get the content of subdir (including its subfolders) without creating the topdir/subdir folders?

Would calling 'unzip' be a better alternative?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Glenn
  • 1
  • 1
  • Try to specify this question to one question, you can ask how to extract a zip folder or how to delete 4 folders but normally you can figure out one of them on your own and only need to ask one question, otherwise you can ask two separate questions – Hippolippo Feb 18 '20 at 12:31
  • Does this answer your question? [Unzipping files in Python](https://stackoverflow.com/questions/3451111/unzipping-files-in-python) – Hippolippo Feb 18 '20 at 12:32
  • Thanks! I see how my question could need some editing. I've rephrased it, and hopefully it's more readable now. I can successfully extract my zip's, but I don't want to keep the top directories. – Glenn Feb 18 '20 at 12:48

0 Answers0