What's the best way to read a text file from within a tar archive as strings (not byte strings)?

Solution 1:

The with statement allows for multiple context managers, and as it turns out, their construction may depend on previous ones in the chain - example:

class manager:
    def __init__(self, name, child=None):
        self.name, self.child = name, child
    def __exit__(self, t, value, traceback):
        print('exiting', self)
    def __enter__(self):
        print('entering', self)
        return self
    def __str__(self):
        childname = None if self.child is None else f"'{self.child.name}'"
        return f"manager '{self.name}' with child {childname}"

Testing it:

>>> with manager('x') as x, manager('y', x) as y, manager('z', y) as z: pass
...
entering manager 'x' with child None
entering manager 'y' with child 'x'
entering manager 'z' with child 'y'
exiting manager 'z' with child 'y'
exiting manager 'y' with child 'x'
exiting manager 'x' with child None

Thus:

with tarfile.open("file.tar") as tar, tar.extractfile("test.txt") as binary, io.TextIOWrapper(binary) as text:
    lines = text.readlines()

(Although I don't think you really need to manage all those contexts anyway...)