What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to Work with ZIP Files in Python? (Open, Read & Extract)

  • Apr 24, 2023
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Kusum Jain
How to Work with ZIP Files in Python? (Open, Read & Extract)

Zip is a popular archive format used to compress and package files, and Python is one of the most popular programming languages. So, you just need to know how to work with both of them. In this article, we will learn how to import, open, read, and extract zip files in Python.

What is zipfile in Python?

The zipfile module in Python provides a way to create, read, and extract zip files programmatically. It is a built-in module that provides various methods to work with these compressed files.

To import the zipfile module,  you can simply use the import statement:

import zipfile

 

But we can use the ZipFile class only in our code without having to type out the full module name every time.  The line "from zipfile import ZipFile" is a Python import statement that allows a program to access the ZipFile class from the zipfile module. The ZipFile class is one of the main classes in the module and provides methods for working with ZIP files.

How to open a zip file in Python?

To open the file we use the open() function. The open() function is a built-in Python function that takes two arguments: the first argument is the name of the file to be opened, and the second argument specifies the mode in which the file should be opened.

with open('ab.py', 'r') as f:

 print(f.readlines())

 

The code "with open('ab.py', 'r') as f: # print(f.readlines())" opens the file ab.py in read mode using a with the statement in Python. The with statement is a context manager that ensures that the file is properly closed when the block of code within the with statement is finished executing.

In this case, the file is opened in read mode, which allows us to read the contents of the file but not modify it. In this case, the mode 'r' indicates that the file should be opened in read mode. The as f statement assigns the file object returned by the open() function to the variable f, which we can then use to access the contents of the file.

The print(f.readlines()) statement reads all the lines in the file ab.py using the readlines() method of the file object f, and prints Zip file 2 to the console. The readlines() method returns a list of strings, where each string is a line in the file.

Creating a zip file with parameters

 To create a new zip file we can create a new instance of the ZipFile class and specify the name of the zip file that we want to create.

After that, we can add files to the zip file using the write() method of the ZipFile object. Finally, we can close the zip file using the close() method of the ZipFile object. When adding files to a zip file, we can specify the name and path of the file that we want to add.

We can also specify the compression method that we want to use for each file. Creating zip files in Python can be useful for a variety of tasks, such as compressing and archiving multiple files, creating backups, and distributing files in a compressed format.

Here is an example:

with ZipFile('aa.zip',mode='w') as zf:

 zf.write('ab.py')

 zf.write('ac.txt')

# Print a table of contents in the directory

 zf.printdir()

 

Output:

File Name Size Modified

ab.py 592 2023-02-28 12:01:56

ac.txt 1 2023-03-19 13:27:07

 

The given code uses the ZipFile class from the zipfile module in Python to create a new ZIP file named aa.zip , add two files named ab.py and ac.txt to it, and print a table of contents for the ZIP file. The first line of code, with ZipFile('aa.zip', mode='w') as zf: , creates a new instance of the ZipFile class and opens the file aa.zip in write mode.

The with statement ensures that the file is properly closed after it has been used, even if an exception is raised. The next two lines of code, zf.write('ab.py') and zf.write('ac.txt') , add the files ab.py and ac.txt to the ZIP file aa.zip .

The write method of the ZipFile object is used to add each file to the ZIP archive. Finally, the line zf.printdir() prints a table of contents for the ZIP file aa.zip . This table of contents lists the names, sizes, and modification times of all the files in the archive.

How to read a zip file in Python?

To read the contents of a ZIP file, we can use the ZipFile class from the built-in zipfile module in Python.

First, import the zipfile module. Then, we can create a new instance of the ZipFile class and specify the name and path of the zip file that we want to read. After that, we can use the methods provided by the ZipFile class to access the contents of the zip file.

The ZipFile class provides several methods for reading zip files, such as namelist() , infolist() , read() , and extract() . The namelist() method returns a list of all the file names in the zip archive, while the infolist() method returns a list of ZipInfo objects for all the files in the archive.

The read() method can be used to read the contents of a specific file in the zip archive, while the extract() method can be used to extract a specific file from the archive and save it to a specified location Zip file 3 on the filesystem.

When reading a zip file in Python, it is important to be aware of the file paths and naming conventions used in the archive, as these may differ from the paths and naming conventions used on the local filesystem. In addition, some zip archives may be password-protected or encrypted, which may require additional steps to read or extract the contents of the archive.

Check this example for a better understanding:

with ZipFile('aa.zip') as myzip:

 with myzip.open('ac.txt') as myfile:

      print(myfile.readline())

 

Output:

`s`[b'aa\n', b'hello world\n', b'\n']

 

The code snippet creates a context manager using the with statement to open a zip file named aa.zip using the ZipFile class from the zipfile module in Python. It then opens a file named ac.txt that is stored within the zip archive using the open() method of the ZipFile object.

Once the file is open, it reads the first line of the file using the readline() method of the file object and prints it to the console using the print() statement.

How Extract a zip file in Python?

To extract a zip file in Python, we can first import the zipfile module. Then, we can create a new instance of the ZipFile class and specify the name and path of the zip file that we want to extract. After that, we can use the extractall() method of the ZipFile class to extract all the files in the archive to a specified location on the filesystem.

Alternatively, we can use the extract() method of the ZipFile class to extract individual files from the archive to a specified location on the filesystem. When extracting a zip file in Python, it is important to be aware of the file paths and naming conventions used in the archive, as these may differ from the paths and naming conventions used on the local filesystem.

In addition, some zip archives may be password-protected or encrypted, which may require additional steps to extract the contents of the archive. Extracting a zip file in Python can be useful for a variety of tasks, such as decompressing and unpacking files, installing software packages, and restoring backups.

Here is examples to unzip a zip file in python:

with ZipFile(aa.zip, 'r') as zip:

 # printing all the contents of the zip file

  zip.printdir()

# extracting all the files in current working directory 

  zip.extractall()

 

This code snippet opens a zip file named aa.zip in read mode. It then prints the directory structure and file information of the zip file using the printdir() method of the ZipFile object. After that, it extracts all the files contained in the zip file to the current working directory using the extractall() method of the ZipFile object.

with ZipFile('aa.zip', 'r') as zip:

# printing all the contents of the zip file

   zip.printdir()

# extracting

   zip.extract(ab.py)

 

This code snippet opens a zip file named aa.zip in read mode using the ZipFile class from the zipfile module in Python. It then prints the directory structure and file information of the zip file using the printdir() method of the ZipFile object. After that, it extracts a file named ab.py from the zip file to the current working directory using the extract() method of the ZipFile object.

Conclusion

In conclusion, we learned about the ZipFile module in Python provides a convenient and efficient way to work with ZIP files. It allows users to create, read, and extract ZIP files, as well as manipulate the contents of these archives. Happy Learning :)

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Kusum Jain
I am an avid coder on CodeChef, I partake in hackathons regularly. I am an independent and self-motivated student with a love for coding and experiencing new things.