I want to create a script for unzip (.tar.gz) file via (Python)
Why do you want to "press" twice to extract a .tar.gz, when you can easily do it once? Here is a simple code to extract both .tar and .tar.gz in one go:
import tarfileif fname.endswith("tar.gz"): tar = tarfile.open(fname, "r:gz") tar.extractall() tar.close()elif fname.endswith("tar"): tar = tarfile.open(fname, "r:") tar.extractall() tar.close()
If you are using python 3, you should use shutil.unpack_archive that works for most of the common archive format.
shutil.unpack_archive(filename[, extract_dir[, format]])
Unpack an archive. filename is the full path of the archive. extract_dir is the name of the target directory where the archive is unpacked. If not provided, the current working directory is used.
For example:
def extract_all(archives, extract_path): for filename in archives: shutil.unpack_archive(filename, extract_path)