|
- from __future__ import print_function
-
- import os.path as osp
- import tarfile
- import zipfile
- import bz2
- import gzip
-
-
- def maybe_log(path, log=True):
- if log:
- print("Extracting", path)
-
-
- def extract_tar(path, folder, mode="r:gz", log=True):
- r"""Extracts a tar archive to a specific folder.
-
- Args:
- path (string): The path to the tar archive.
- folder (string): The folder.
- mode (string, optional): The compression mode. (default: :obj:`"r:gz"`)
- log (bool, optional): If :obj:`False`, will not print anything to the
- console. (default: :obj:`True`)
- """
- maybe_log(path, log)
- with tarfile.open(path, mode) as f:
- f.extractall(folder)
-
-
- def extract_zip(path, folder, log=True):
- r"""Extracts a zip archive to a specific folder.
-
- Args:
- path (string): The path to the tar archive.
- folder (string): The folder.
- log (bool, optional): If :obj:`False`, will not print anything to the
- console. (default: :obj:`True`)
- """
- maybe_log(path, log)
- with zipfile.ZipFile(path, "r") as f:
- f.extractall(folder)
-
-
- def extract_bz2(path, folder, log=True):
- maybe_log(path, log)
- with bz2.open(path, "r") as r:
- with open(osp.join(folder, ".".join(path.split(".")[:-1])), "wb") as w:
- w.write(r.read())
-
-
- def extract_gz(path, folder, log=True):
- maybe_log(path, log)
- with gzip.open(path, "r") as r:
- with open(osp.join(folder, ".".join(path.split(".")[:-1])), "wb") as w:
- w.write(r.read())
|