Path: blob/master/12-Advanced Python Modules/07-Unzipping-and-Zipping-Files.ipynb
666 views
Unzipping and Zipping Files
As you are probably aware, files can be compressed to a zip format. Often people use special programs on their computer to unzip these files, luckily for us, Python can do the same task with just a few simple lines of code.
Create Files to Compress
Zipping Files
The zipfile library is built in to Python, we can use it to compress folders or files. To compress all files in a folder, just use the os.walk() method to iterate this process for all the files in a directory.
Create Zip file first , then write to it (the write step compresses the files.)
Extracting from Zip Files
We can easily extract files with either the extractall() method to get all the files, or just using the extract() method to only grab individual files.
Using shutil library
Often you don't want to extract or archive individual files from a .zip, but instead archive everything at once. The shutil library that is built in to python has easy to use commands for this:
The shutil library can accept a format parameter, format
is the archive format: one of "zip", "tar", "gztar", "bztar", or "xztar".