Looping through and Unzipping .EGP Files with Python

Max Bade
Dev Genius
Published in
3 min readSep 30, 2020

--

This is a zipper!

What is an .EGP File?

Taken straight from StackOverflow:

A .egp file is a zipped set of xml files (and other things). If you change the extension to .zip, you can open it with any unzipping program, and see the contents. It is possible to extract programs and such from there at that point, though it’s not necessarily easy to do as it’s fairly messy.

Why am I writing this story?

For one simple reason. Because I was tasked with looping through a folder of a bunch of .egp files this week, didn’t know how to do that, figured it out, didn’t want others to have to waste time figuring out something I already did, thus the reason for this post.

Ok, Where’s the code then?

Straight to the point. I like it. Who am I talking to… never mind:

Script 1 of 3:

Here’s a small script to just unzip one .egp file and place the contents in a specified directory. Just change the paths in this script to reproduce:

#!/usr/bin/env python
# coding: utf-8
#script 1
#extract single .egp file based on directory variable
zip_folder = r'folder/with/all/zip/files'
destination = r'destination/folder'
# pwd = '<YOUR_PASSWORD>'
with zipfile.ZipFile(zip_folder) as zf:
zf.extractall(
destination) #pwd=pwd.encode())

Script 2 of 3:

Here’s a script to identify and filter out files (.egp in our case) and loop through and unzip them based on a the specified extension, then place those files in a specified directory:

#get files from directory based on list of extentsions
import os
import zipfile
#create directory and extension variables
rootdir = r'folder/with/all/zip/files'
destination = r'destination/folder'
extensions = ('.egp', '.sas', '.xml')
#loop through the directory/subdirectories and extract .egp files
for subdir, dirs, files in os.walk(rootdir):
for file in files:
ext = os.path.splitext(file)[-1].lower()
if ext == '.egp': #change to other extension if necessary
files = os.path.join(subdir, file)
print(files)
# pwd = '<YOUR_PASSWORD>' #insert password if directory is password protected
with zipfile.ZipFile(files) as zf:
zf.extractall(
destination) #pwd=pwd.encode())
Script 2 will produce something like this

Script 3 of 3:

A simple way to view the contents of what you just extracted — all the files and folders:

#list all files and folders in a specific folder and add them to a list
from os import listdir
from os.path import isfile, isdir, join
#get only files or only dirs
onlyfiles = [f for f in listdir(destination) if isfile(join(destination, f))]
onlydirs = [f for f in listdir(destination) if isdir(join(destination, f))]
print('All Files:')
for i in onlyfiles:
print(i)
print('')
print('All Folders:')
for i in onlydirs:
print(i)
Script 3 will produce something like this

The Full Script

Because everyone wants to see the script in color:

Regards,

Max

--

--