unzip

pyhelpers.store.unzip(path_to_zip_file, output_dir=None, ret_output_dir=False, verbose=False, raise_error=False, **kwargs)[source]

Unzips data from a Zip (compressed) file.

Parameters:
  • path_to_zip_file (str | os.PathLike) – The path where the Zip file is saved.

  • output_dir (str | None) – The directory where the extracted data will be saved; defaults to None.

  • ret_output_dir (bool) – Whether to return the path to output directory; defaults to False.

  • verbose (bool | int) – Whether to print relevant information to the console; defaults to False.

  • raise_error – Whether to raise the provided exception; if raise_error=False (default), the error will be suppressed.

  • kwargs – [Optional] Additional parameters for the method zipfile.ZipFile.extractall().

Examples:

>>> from pyhelpers.store import unzip
>>> from pyhelpers.dirs import cd, delete_dir
>>> zip_file_path = cd("tests", "data", "zipped.zip")
>>> unzip(path_to_zip_file=zip_file_path, verbose=True)
Extracting ".\tests\data\zipped.zip" to ".\tests\data\zipped\" ... Done.
>>> output_dir_1 = cd("tests", "data", "zipped")
>>> out_file_pathname = cd(output_dir_1, "zipped.txt")
>>> with open(out_file_pathname) as f:
...     print(f.read())
test
>>> output_dir_2 = cd("tests", "data", "zipped_alt")
>>> unzip(path_to_zip_file=zip_file_path, output_dir=output_dir_2, verbose=True)
Extracting ".\tests\data\zipped.zip" to ".\tests\data\zipped_alt\" ... Done.
>>> out_file_pathname = cd(output_dir_2, "zipped.txt")
>>> with open(out_file_pathname) as f:
...     print(f.read())
test
>>> # Delete the directories ".\tests\data\zipped\" and ".\tests\data\zipped_alt\"
>>> delete_dir([output_dir_1, output_dir_2], verbose=True)
To delete the following directories:
    ".\tests\data\zipped\" (Not empty)
    ".\tests\data\zipped_alt\" (Not empty)
? [No]|Yes: yes
Deleting ".\tests\data\zipped\" ... Done.
Deleting ".\tests\data\zipped_alt\" ... Done.