Menu Close

How to unzip files in Python?

Sometimes, we want to unzip files in Python.

In this article, we’ll look at how to unzip files in Python.

How to unzip files in Python?

To unzip files in Python, we can use the zipfile module.

For instance, we write

import zipfile

with zipfile.ZipFile(path_to_zip_file, 'r') as zip_ref:
    zip_ref.extractall(directory_to_extract_to)

to create a ZipFile object to open the path_to_zip_file file with read permission.

Then we call zip_ref.extractall with the directory_to_extract_to string to extract the files in the zip file into the directory_to_extract_to directory.

Conclusion

To unzip files in Python, we can use the zipfile module.

Posted in Python, Python Answers