Related to the popular recipe 442503, this command-line program takes multiple TAR files or directories and can extract them in a batch operation. This is committed for archival to be run under Python 2.5 or later versions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import os
import sys
import tarfile
def main():
total = untar(sys.argv[1:])
if total:
args = total, total > 1 and 's were' or ' was'
sys.stdout.write('Report: %s file%s untared.' % args)
else:
filename = os.path.basename(sys.argv[0])
sys.stdout.write('Usage: %s <file_or_dir> ...' % filename)
def untar(paths):
total = 0
for path in paths:
if os.path.isdir(path):
try:
dir_list = os.listdir(path)
except:
pass
else:
total += untar(os.path.join(path, new) for new in dir_list)
elif os.path.isfile(path):
try:
tarfile.open(path).extractall(os.path.dirname(path))
except:
pass
else:
total += 1
return total
if __name__ == '__main__':
main()
|
This is simply a more powerful (and larger) version of the original utility.