Tarfile_TarInfo Objects














































Tarfile_TarInfo Objects



TarInfo Objects


TarInfo object represents one member in a TarFile. Aside from storing all required attributes of a file (like file type, size, time, permissions, owner etc.), it provides some useful methods to determine its type. It does not contain the file's data itself.

TarInfo objects are returned by TarFile's methods getmember()getmembers() and gettarinfo()


  • class TarInfo([name])-
                           Create a TarInfo object.

  • frombuf()-
                          Create and return a TarInfo object from a string buffer.

  • tobuf()-
                                 Create a string buffer from a TarInfo object.

TarInfo object has the following public data attributes:

                             please, read my previous module Library Tarfile. In which I have discussed all the attributes.

  • type-                           
                   File type.type is usually one of these constants:     

           * REGTYPE 

          * AREGTYPE 

          * LNKTYPE 

          * SYMTYPE

          * DIRTYPE

          * FIFOTYPE

          * CONTTYPE

          * CHRTYPE

          * BLKTYPE

          * GNUTYPE_SPARSE

--> To determine the type of a TarInfo object more conveniently, use the is_*() methods.


  • linkname-
                          Name of the target file name, which is only present in TarInfo objects of type LNKTYPE and SYMTYPE.


  • pax_headers- 
                           A dictionary containing key-value pairs of an associated pax extended header.


EXAMPLE:-

CODE-

import tarfile

def hello(tar):
    
    for tarinfo in tar:
        print (tarinfo.name, "is", tarinfo.size, "bytes in size and is",)
        
        if tarinfo.isreg():
            print ("a regular file.")
            
        elif tarinfo.isdir():
            print ("a directory.")
            
        else:
            print ("something else.")
            
    tar.close()

tar = tarfile.open("phayes-geoPHP-1.2-20-g6855624.tar.gz", "r:gz")
hello(tar)

OUTPUT-






Comments