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()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With