Create a Python Script to Extract all Zip Files in a Directory

Create a Python Script to Extract all Zip Files in a Directory

Overview

This article explains how to write a Python script that extracts all zip files in any directory.

Materials

  • Computer

  • Python

    • Python Packages used: os, glob, Zipfile
  • Command Prompt, Powershell, Terminal or IDE

Procedure: Writing the Script

Step 1: Import the Modules

This script uses three modules: os, glob and ZipFile. No packages are needed to be downloaded in order to use these three modules in Python. Below is the syntax of how to import each module:

#!/usr/bin/env python3

import os
import glob
from zipfile import ZipFile

What Each Module Does

os - Allows to change directories in a Python Script

glob - Searches for files with a certain extension, in this case, the script will include code to search for any zip files in a directory

Zipfile - This module extracts any zip files in a directory

Step 2: Change Directory

A variable named "path" was declared and set equal to "os.getcwd()". The os module is used for the command "os.getcwd()". cwd stands for current working directory, by using "os.getcwd()", the os module command will get the current working directory path. This variable is important since more code will be used to verify if the script is being run in the same directory that contains this Python script.

#!/usr/bin/env python3
import os
import glob
from zipfile import ZipFile

path = os.getcwd()

Step 3: Search for Zip Files

The glob module allows to search for any file by specifying its name and file extension. A variable, in this case, named files is declared and equal to glob.glob(''). Within the parenthesis, the name and file extension can be written out to search for that specific zip file. A directory can have more than one zip file, therefore, by specifying '*.zip', it will search for every file name that has the .zip extension.

The glob.glob() always creates a list of all the file names that fit the criteria it is looking for.

#!/usr/bin/env python3
import os
import glob
from zipfile import ZipFile

path = os.getcwd()
files = glob.glob('*.zip')

Step 4: Finishing the Script by Incorporating a For Loop

A variable named index has been declared to initiate through a for loop. The for loop is placed within an if statement. The if statement must satisfy the condition if "path == r"Enter directory here" ", then the for loop will run. This if statement checks the "path" variable that was already declared is seeing if the current present directory equals the written directory that should be written instead of "Enter directory here", will run the for loop. This script is meant to be run in the "Downloads" folder as zip files are downloaded into that directory, but it can be chosen to be run in any directory. The r before the "" is necessary as it reads any string raw or as it is written. Without the r, it would not read the path correctly since Python does not interpret single backslashes / or \ in quotes.

The for loop reads every element in the "files" variable. To use ZipFile to extract files, the syntax to declare it is shown below. It asks for the file name that needs to be extracted and the mode. files[index] is written there since "files" is a list variable that contains all the names of the zip files, so by declaring index to be a variable that increases by 1 gradually over time, it will access every zip file name within files. mode="r" means to read every file. After the for loop and ZipFile line of code obtain the name of the zip file name, the line "zip.extractall()" will extract the file with that name. The next line of code just prints out the confirmation that the name of the file has been successfully extracted. The for loop will run in conjunction with the "with ZipFile" line of code to detect and extract all zip files in a directory.

#!/usr/bin/env python3
# Finalized Script                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

import os
import glob
from zipfile import ZipFile

path = os.getcwd()
files = glob.glob('*.zip')
index = 0

if path == r"*Enter directory here*":
    for i in range(0, len(files)):
        with ZipFile(files[index], mode='r') as zip:
            zip.extractall()
            print("Extraction completed! " + str(files[index]) + " has been extracted!")
        index += 1
else:
    print("Not the specified directory. Go to the specified directory and it must contain this Python Script to extract all zip files.")

Demonstration

Below shows how to run the Python script. The downloads directory contains the Python script and some zip files before running the Python file:

Source Code

https://github.com/AndrewDass1/SCRIPTS/tree/main/Python/Extract%20Zip%20Files