Is_tarfile () returns True for an empty file

EDIT 1

Hm, I accept the answers that tar respects an empty file ... but on my system:

$ touch emptytar $ tar -tf emptytar tar: This does not look like a tar archive tar: Exiting with failure status due to previous errors 

Maybe I have a non-canonical version?

 $ tar --version tar (GNU tar) 1.22 Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by John Gilmore and Jay Fenlason. 

Hello to all,

I am testing some logic to handle the user loading the tar file. When I write an empty file in tarfile.is_tarfile() , it returns True , which I do not expect:

 $ touch tartest $ cat tartest $ python -c "import tarfile; print tarfile.is_tarfile('tartest')" True 

If I add some text to the file, it will return False , which I expect:

 $ 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 the file with zero length, but based on the documentation for tarfile.is_tarfile(name) I think this is not necessary:

Return True if the name is the tar archive of the file that the tarfile module can use. to read.

I went so far as to check the source, tarfile.py , and I see that it is checking header blocks, but I don’t quite understand how it evaluates these blocks.

Am I misunderstanding the documentation and therefore setting unfair expectations?

Thanks,
Zachary

+7
python
source share
4 answers

Try this on the command line:

 $ touch emptyfile $ tar -tvf emptyfile 

No mistakes.

It seems that an empty file is simply a valid (but useless) TAR file.

+1
source share

An empty tar file is a perfectly valid and empty tar file. Consider at any Unix shell prompt:

 $ touch foo.tar $ ls -l foo.tar -rw-r--r-- 1 aleax staff 0 Jun 16 18:49 foo.tar $ tar tvf foo.tar $ tar xvf foo.tar 

Cm? An empty foo.tar is a valid tar file for the Unix tar command - it just has nothing to show or unzip. It would be really problematic if Python tar handling was so dramatically different from tar ! What suggestion in the documents made you believe that such a problematic, incompatible with a headache incompatibility is part of the specifications?

+4
source share

Actually, the behavior of "is_tarfile" seems to have changed between Python 2.6 and 2.7. In Python 2.7, is_tarfile returns False for an empty file.

 $ touch /tmp/foo.tar $ python Python 2.7.3 (default, Jul 24 2012, 11:41:40) [GCC 4.6.3 20120306 (Red Hat 4.6.3-2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import tarfile >>> print tarfile.is_tarfile("/tmp/foo.tar") False >>> $ 
+1
source share

This is a fundamental feature of logic.

The default value is "True" until the contents of the file are proven false.

No content, no refutation of the assumption.

-one
source share

All Articles