2

I want to unzip particular subfolders from a list of zip archives using the zipfile module.

Paths to archives are stored in a csv file with concatenated subfolders, like so:

C:/path_to/archive.zip/y/z/subfolder_a
C:/path_to_a_different/archive.zip/x/subfolder_b
...

My goal is to unzip only these specific subfolders to another location while maintaining file structure. Here is what I have come up with.

import zipfile
import os

# List of archive paths with subfolder location
paths = 'C:\\paths.csv'
paths = open(paths, 'r')

for path in paths:
    archive = path.split('.zip')[0] + '.zip'
    path_to_subfolder = path.split('.zip')[1].rstrip('\n')

    # Removes leading slash and replaces backslashes by slashes
    path_to_subfolder = path_to_subfolder.strip('\\').replace('\\','/')

    # Defines name of output folder
    output_folder_name = os.path.split(path_to_subfolder)[-1].rstrip('\n') # subfolder_a, subfolder_b

    # Moves relevant subfolders to output location
    archive = zipfile.ZipFile(archive)
    for file in archive.namelist():
        if file.startswith(path_to_subfolder):
            archive.extract(file, 'C://output//{}'.format(output_folder_name))

The folder structure after running this looks like:

C:/output/subfolder_a/y/z/subfolder_a/...

but the folder structure should look like this:

C:/output/subfolder_a/...

What can I change to fix this?

Dharman
  • 30,962
  • 25
  • 85
  • 135
llkrht
  • 21
  • 3
  • maybe first use `print()` to see what you have in variables. Especially what you have in `'C://output//{}'.format(output_folder_name)` – furas May 10 '21 at 19:57
  • you may have to create full path with `filename` for every `file` but you create full path without `filename`. Maybe you should simply use `replace` like `file.replace(path_to_subfolder, f"C://output//{output_folder_name}")` – furas May 10 '21 at 20:02

0 Answers0