I don\'t want to use OS commands as that makes it is OS dependent.
This is available in tarfile
, tarfile.is_tarfile(filename)
, to check if
I had a few hundred thousand gzip files, only a few of which are zero-sized, mounted on a network share. I was forced to use the following optimization. It is brittle, but in the (very frequent) case in which you have a large number of files generated using the same method, the sum of all the bytes other than the name of the payload are a constant.
Then you can check for a zero-sized payload by:
from os import stat
from os.path import basename
# YMMV with len_minus_file_name
def is_gzip_empty(file_name, len_minus_file_name=23):
return os.stat(file_name).st_size - len(basename(file_name)) == len_minus_file_name
This could break in many ways. Caveat emptor. Only use it if other methods are not practical.