I’m using the code below to extract .tgz files. The type of log files (.tgz) that I need to extract have sub-directories that have other .tgz files and .tar files inside them. I want to extract those too.
Ultimately, I’m trying to search for certain strings in all .log files and .txt files that may appear in a .tgz file.
Below is the code that I’m using to extract the .tgz file. I’ve been trying to work out how to extract the sub-files (.tgz and .tar). So far, I’ve been unsuccessful.
import os, sys, tarfile
try:
tar = tarfile.open(sys.argv[1] + '.tgz', 'r:gz')
for item in tar:
tar.extract(item)
print 'Done.'
except:
name = os.path.basename(sys.argv[0])
print name[:name.rfind('.')], '<filename>'
This should give you the desired result:
As @cularis said this is called recursion.