KZiovas
KZiovas

Reputation: 4709

How to check if a tar file is not empty before unpacking it with Python 3?

I wish to unpack some tar archives but I only want to rpcess non-empty ones. I found some code for gzip archives How to check empty gzip file in Python and also this:

async def is_nonempty_tar_file(self, tarfile):
    with open(tarfile, "rb") as f:
        try:
            file_content = f.read(1)
            return len(file_content) > 1
        except Exception as exc:
            self.logger.error(
                f"Reading tarfile failed for {tarfile}", exc_info=True
            )

All the tar archives both empty and non-empty ones seem to have at least thsi character in them \x1f. SO they all pass the test even if they are empty.

How else can I check this?

Upvotes: 1

Views: 1141

Answers (3)

href_
href_

Reputation: 1589

If you want to avoid listing all members (which can be costly on a large tarfile), you can also check if there is at least one member:

import tarfile

tar = tarfile.open("the_file.tar")

if tar.next() is None:
    print("The tarfile is empty")
else:
    print("The tarfile has at least one member")

At least in my tests, it seems that this does not impact a later call to tar.extractall(), so the tar.next() call does not seem to advance the position in a way that impacts that, as the name next might indicate.

Upvotes: 0

KZiovas
KZiovas

Reputation: 4709

OK I found a way using the getmembers() method from tarfile module. I made this method that checks for non empty tarfiles:

 def is_nonempty_tar_file(self, archive):
    with tarfile.open(archive, "r") as tar:
        try:
            file_content = tar.getmembers()
            return len(file_content) > 0
        except Exception as exc:
            print(f"Reading tarfile failed for {archive}")

Upvotes: 1

sarema
sarema

Reputation: 725

You can list contents of tarfiles with the tarfile module:

https://docs.python.org/3/library/tarfile.html#command-line-options

You probably can just use tarfile.open and check if the descriptor contains anything.

import tarfile

x = tarfile.open("the_file.tar")
x.list()

Upvotes: 1

Related Questions