I want to take an arbitrary number of paths representing nested tar archives and perform an operation on the innermost archive. The problem is that nesting can be arbitrary, so the number of context managers I need is also arbitrary.
Take for example:
ARCHIVE_PATH = "path/to/archive.tar"
INNER_PATHS = (
"nested/within/archive/one.tar",
"nested/within/archive/two.tar",
)
def list_inner_contents(archive_path, inner_paths):
with TarFile(archive_path) as tf1:
with TarFile(fileobj=tf1.extractfile(inner_paths[0])) as tf2:
with TarFile(fileobj=tf2.extractfile(inner_paths[1])) as tf3:
return tfX.getnames()
contents = list_inner_contents(ARCHIVE_PATH, INNER_PATHS))
I cannot use the with nesting syntax operator , since there can be any number of levels in a socket. I can't use contextlib.nestedit because the docs say right here:
... using nested()two files to open is a programming error, since the first file will not be closed promptly if an exception is thrown when opening the second file.
?