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
- Import Python’s zipfile module into code using import zipfile statement
- Call ZipFile(filename) Function of class zipfile using zipfile.ZipFile(filename)
- ZipFile(filename) Function will return an extractable object
- 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
I hope that this short tutorial helped you to understand How to unzip files using Python Programming Language.