Unzipping Files Using Python

Python is a quite simple and powerful programming language in the sense that it can be applied to so many areas like Scientific Computing, Natural Language Processing but one specific area of application of Python which I found quite fascinating is => Using Python for dealing with Files and FileSystem in an Operating System.

In this article, I’ll discuss How to unzip files using Python Programming Language.

Let’s see what steps need to be followed for unzipping files using Python Programming Language

  1. Import Python’s zipfile module into code using import zipfile statement
  2. Call ZipFile(filename) Function of class zipfile using zipfile.ZipFile(filename)
  3. ZipFile(filename) Function will return an extractable object
  4. Extract files from this extractable object using extractall() function

That’s it, above are four steps which need to be followed for unzipping files using Python Programming Language. Let’s put all of these four steps together as Python Code for Unzipping Files.

import zipfile
 
# Requires a zip archive called 'test.zip' in current directory.
un_zip = zipfile.ZipFile('test.zip')
 
# Will unzip contents of test.zip in same directory as .zip file.
un_zip.extractall()
 
# This will print out files found in the archive.
print (un_zip.namelist())

Example => Showing step-by-step unzipping of file using Python

  • Do not that Python and file to be unzipped should be in same folder
  • Run the Python Program
  • Same Folder as Python Code File Will Contain Unzipped File somefile.txt
Unzipping File using Python Programming Language

I hope that this short tutorial helped you to understand How to unzip files using Python Programming Language.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts