Zip, Struggle, and Progress

Anterra Kennedy
Atha Data Science
Published in
3 min readJul 7, 2020

April 18th, 2020

Just under a week now spent learning and working in Python, and with each passing day I’m more sure this is the path for me.

A lot of new skills have been acquired and developed in the last week, and I’ve slowly become more comfortable working inside Python. Its remarkable how much being inside a program is so like being inside a complicated math or physics problem, with an understanding of all the moving parts and ability to see how they connect and hold many parts at once toward a solution, your thoughts not taking the form of english but of pure logic. I’m thriving activating that part of my brain that hasn’t been truly stimulated in a couple of years.

I established a GitHub, and learned Git from the command line to commit and push version controlled programs up to my new repository with example and practice problems in it. Part of that involved learning to create a .gitignore file so as to avoid adding my entire venv to the repository. I also had the opportunity to relearn binary.

Working through A Byte of Python, I encountered my first challenge — to create a program which would create backups of your specified files and store them in a Zip file. The reason this posed difficulty was because Windows does not have a built in zip function. So, I turned to the third party 7-Zip. I set the path variables so ‘7z’ to zip files should have been executable from anywhere in the command line, but oddly it was only able to run when inside the 7-Zip folder. Some research online yielded this solution:

PATH=%PATH%;C:\Program Files\7-Zip\
echo %PATH%
7z

Which was successful in then allowing the 7z function to be called from anywhere (for instance, from inside the Python zipping program file). After lots of struggle, the program worked! It successfully created backup files and stored them in a zip file when executed from the command line. However, the program still couldn’t be successfully run inside PyCharm itself. Maybe in time I’ll develop enough expertise to get 7-Zip to be usable inside PyCharm — but even after adding it as a ‘tool’ it would not work.

The challenge prompt then asked that the program be rewritten instead using Python’s standard library zipfile function. This I carried out as follows:

with ZipFile(target, “w”) as zip:
for file in file_paths:
zip.write(file)

I had to learn through trial and error that the type of information the zip function needed was the actual paths to the files you want zipped, not just the files themselves. I ended up using os.walk to retrieve file paths to do so. Ultimately, I find using a built-in Python module to complete a task as opposed to relying on external system-specific functionality to be a much cleaner and more preferred solution to any problem. It allows cross-platform ability to run, by just downloading the Python program itself. Always seems best to be self-contained! At least for a simple program such as this.

What all of this demonstrated to me was some real world problem solving and debugging, and it was a joy!

--

--