Do you need to unzip files with Python keeping or not the file structure? In this short article you can find several examples on extracting single or multiple files to the same folder.

Step 1: Unzip single file from zip archive

To start, let's have a zip file called: archive.zip. The file is available from here: Stock Exchange Data.

It has 3 csv files inside:

  • indexProcessed.csv
  • indexInfo.csv
  • indexData.csv

If you like to extract only one file from it - indexProcessed.csv then you can use next Python snippet:

import zipfile

path = '/home/myuser/Downloads/'

archive = zipfile.ZipFile(f'{path}archive.zip')

for file in archive.namelist():
    if file.startswith('indexProcessed.csv'):
        archive.extract(file, path)

The archive is located in the folder: /home/myuser/Downloads/ and the destination file will be extracted in the same place.

The code is using the library zipfile. It reads the archive and iterates over all files inside.

Then checks if the file starts with indexProcessed.csv if so it will extract it to the destination path.

In order to extract multiple files you can change the if condition to meet your needs.

Step 2: Unzip all files from zip archive

What if you like to** extract all files from a zip archive**. Again we are going to use zipfile. This time instead of method extract we will use extractall:

import zipfile

path = '/home/myuser/Downloads/'

zipfilepath = f'{path}archive.zip'
extractiondir = f'{path}/extract'

zip = zipfile.ZipFile(zipfilepath)
zip.extractall(path=extractiondir)

Again the archive file is located in /home/myuser/Downloads/. The output folder will be /home/myuser/Downloads/extract

Note that this method will extract the whole structure - all files and folder recursively.

unzip-one-multiple-zipped-files-archive-python

Step 3: Unzip all files without keeping the structure

To unzip all files from a zip file without keeping the original structure you can use module os in order to change the destination folder as:

import zipfile
import os

path = '/home/myuser/Downloads/'
archive = f'{path}/archive.zip'

with zipfile.ZipFile(archive) as zipfile:
    for zip_info in zipfile.infolist():
        if zip_info.filename[-1] == '/':
            continue
        zip_info.filename = os.path.basename(zip_info.filename)
        zipfile.extract(zip_info, f'{path}/extract')

The code above will extract all files into the destination folder /home/myuser/Downloads/extract recursively.

Step 4: Unzip all CSV files from zip archive

Finally let's check how to extract only CSV files from archive. This can be done by filtering files by CSV extension. We will use method endswith:

import zipfile

path = '/home/myuser/Downloads/'

archive = zipfile.ZipFile(f'{path}archive.zip')

for file in archive.namelist():
    if file.endswith('.csv'):
        archive.extract(file, path)