In python, if extract a tar.gz file, how to get or set the name of the result file

13.1k views Asked by At

My question is like: when use:

import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()

if the file before compress called "sampleFolder", after I doing the above steps, how to return the "sampleFolder" name, better with its full path, or how to set the result to other name like "Folder"?

it's not a good question, but I actually have demand on this in my project.

I have to edit the question to: if I don't know the "sampleFolder", can I get a return to it after the decompress step

2

There are 2 answers

5
wim On BEST ANSWER

It will be extracted to the working directory by default:

import os
os.getcwd()

So, the path to the extracted data is:

from pathlib import Path
extracted_to_path = Path.cwd() / 'sampleFolder'

To extract in a different location:

with tarfile.open('sample.tar.gz') as tar:
    tar.extractall(path='/other/folder')

edit: If you just want to know the name "sampleFolder" contained in the archive, it's not necessary to extract somewhere. You should use getnames:

tar.getnames()

Note that tarballs can have multiple files or folders within.

0
bhargav3vedi On

Python provides shutil library which can extract zip files.

import shutil
help(shutil.unpack_archive)

unpack_archive(filename, extract_dir=None, format=None) Unpack an archive.

`filename` is the name 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.

`format` is the archive format: one of "zip", "tar", "gztar", "bztar",
or "xztar".  Or any other registered format.  If not provided,
unpack_archive will use the filename extension and see if an unpacker
was registered for that extension.

In case none is found, a ValueError is raised.
shutil.unpack_archive(filename="file_path", extract_dir="path_where_you_want_extracted_file", format="tar/zip")