Neemaximo
Neemaximo

Reputation: 20771

How can I extract all .tar.gz files while maintaining the directory structure?

I am working on a program to unzip/extract all .tar.gz files in a given folder. This folder can have several sub directories with multiple .tar.gz files as well. I am trying to extract all of them, while maintaining the folder structure, but running into some issues.

My current code is below, extractall() seems to only extract to the current working directory, and I can't quite figure out how to maintain the directory structure.

for zipped_file in pathlib.Path(path).glob('**/*.tar.gz'):
    tar = tarfile.open(zipped_file, 'r:gz')
    tar.extractall()
    tar.close()

Upvotes: 4

Views: 6481

Answers (1)

MarkReedZ
MarkReedZ

Reputation: 1437

https://docs.python.org/3/library/tarfile.html

TarFile.extractall(path=".", members=None, *, numeric_owner=False)
    Extract all members from the archive to the current working directory or directory path.

So:

import os

for path, directories, files in os.walk('/foo/bar'):
    for f in files:
        if f.endswith(".tar.gz"):
            tar = tarfile.open(os.path.join(path,f), 'r:gz')
            tar.extractall(path=path)
            tar.close()

Upvotes: 2

Related Questions