简体   繁体   中英

How can I check if a file exists in a tar archive with Python?

I would like to verify the existence of a given file in a tar archive with Python before I get it as a file-like object. I've tried it with isreg() , but probably I do something wrong.

How can I check if a file exists in a tar archive with Python?

I tried

import tarfile


tar = tarfile.open("sample.tar", "w")
tar.add("test1.txt")
tar.add("test2.txt")
tar.add("test3.py")
tar.close()

tar = tarfile.open("sample.tar", "r")
tai = tar.tarinfo(name="test3.py")
print(tai.isreg())
print(tai.size())
tar.close()

Probably tai is wrong. In fact tai.size() is always 0.

If you really need to check, then you can test for membership using the getnames method and the in operator:

>>> import tarfile
>>> tar = tarfile.open("sample.tar", "w")
>>> "sample.tar" in tar.getnames()
True

However, I think that in Python (and dealing with file systems in general), catching exceptions are preferred. It's better to attempt to read and catch an exception because things can always happen between checking a file's existence and reading it later.

>>> try:
...     tar.getmember('contents.txt')
... except KeyError:
...     pass
...

Maybe use getnames() ?

tar = tarfile.open('sample.tar','r')
if 'test3.py' in tar.getnames():
    print 'test3.py is in sample.tar'

You can use tar.getnames() and the in operator to do it:

$ touch a.txt
$ tar cvf a.tar a.txt
$ python
>>> names = tarfile.open('a.tar').getnames()
>>> 'a.txt' in names
True
>>> 'b.txt' in names
False

This matches even if the tar file has the filename in a subdirectory, and uses normcase to mimic the filename case handling of the current OS (eg on Windows, searching for “readme.txt” should match “README.TXT” inside the tar file).

def filename_in_tar(filename, atarfile):
    filename= os.path.normcase(filename)
    return any(
        filename == os.path.normcase(os.path.basename(tfn))
        for tfn in atarfile.getnames())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM