I am on working on a Python script which is supposed to process a tarball and output new one, trying to keep the format of the original. Thus, I am looking for a way to lookup the compression method used in an open tarball to open the new one with same compression.
AFAICS TarFile class doesn't provide any public interface to get the needed information directly. And I would like to avoid reading the file independently of the tarfile module.
I am currently considering looking up the class of the underlying file object (t.fileobj.__class__) or trying to open the input file in all possible modes and choosing the correct format basing on which one succeeds.
Tar doesn't compress, it concatenates (which is why TarFile won't tell you what compression method is used, because there isn't one). 
Are you trying to find out if it's a tar.gz, tar.bz2, or tar.Z ?
tarfile: determine compression of an open tarball