'How to extract a zip file using python 3

How to extract a zip file using python when zip file present in different directory where script file present. I try this ,but i got error because source path is not accepted ,try to solve me this problem.

from zipfile import ZipFile

def func(source, target):
    with ZipFile('source', 'target'):
        ZipFile.Extractall('target')


Solution 1:[1]

Use this code. To move through directories you could either hard code the directory where your script is present or you could toggle through the directories using simple commands such as "../" to move out of the given directory or "/" to move inside a folder in the directory. For example - "../script.py" or "/folder/script.py". Similarly you can use this to find your .zip file.

import zipfile
with zipfile.ZipFile("file.zip","r") as zip_ref:
    zip_ref.extractall("targetdir")

Solution 2:[2]

You'll have to check for source path of the zip file which is relative to your current working directory. To know your current working directory you can try

import os print(os.getcwd())

zip - Unzipping files in python

relative-paths-in-python

Solution 3:[3]

For just unpacking, shutil should suffice:

import shutil
shutil.unpack_archive('path-to-zipfile')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prakhar Sood
Solution 2 Naman Chikara
Solution 3 00schneider