is_tarfile() returns True for a blank file
        Posted  
        
            by Zachary Young
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Zachary Young
        
        
        
        Published on 2010-06-17T01:40:38Z
        Indexed on 
            2010/06/17
            1:42 UTC
        
        
        Read the original article
        Hit count: 517
        
python
Hello all,
I am testing some logic to handle a user uploading a TAR file.  When I feed a blank file to tarfile.is_tarfile() it returns True, which is not what I am expecting:
$ touch tartest
$ cat tartest
$ python -c "import tarfile; print tarfile.is_tarfile('tartest')"
True
If I add some text to the file, it returns False, which I am expecting:
$ echo "not a tar" > tartest
$ python -c "import tarfile; print tarfile.is_tarfile('tartest')"
False
I could add a check at the beginning to check for a zero-length file, but based on the documentation for tarfile.is_tarfile(name) I think this is unecessary:
Return True if name is a tar archive file, that the tarfile module can read.
I went so far as to check the source, tarfile.py, and I can see that it is checking header blocks but I do not fully understand how it is evaluating those blocks.
Am I misreading the documentation and therefore setting unfair expectations?
Thank you,
Zachary
© Stack Overflow or respective owner