TarFile Object

The TarFile
 object provides an interface to a tar archive. A tar archive is a sequence of blocks. An archive member is made up of a header block followed by data blocks. It is possible, to store a file in a tar archive several times.
                                                  Each archive member is represented by a TarInfo object, see TarInfo Objects for details in my first module Library_Tarfile.

  • The TarInfo argument can be used to replace the default TarInfo class with a different

  • getmember(name)-
                                            Return a TarInfo object for member `name'. If `name' can not be found in the archive, KeyError is raised. If a member occurs more than once in the archive, its last occurence is assumed to be the most up-to-date version.

  • gettarinfo(selfname=Nonearcname=Nonefileobj=None)-

                                                        Create a TarInfo object for either the file `name' or the file object `fileobj'. You can modify some of the TarInfo's attributes before you add it using addfile(). If given,  'arcname' specifies an alternative name for the file in the archive.

  • add(selfnamearcname=Nonerecursive=True)-                                            

                                               Add the file `name' to the archive. `name' may be any type of file (directory, fifo, symbolic link, etc.). If given, `arcname' specifies an alternative name for the file in the archive. Directories are added recursively by default. This can be avoided by setting `recursive' to False.

  • addfile(selftarinfofileobj=None)-

                                                           Add the TarInfo object `tarinfo' to the archive. If `fileobj' is given, tarinfo.size bytes are read from it and added to the archive. You can create TarInfo objects using gettarinfo(). On Windows platforms, `fileobj' should always be opened with mode 'rb' to avoid irritation about the file size.

  • extractfile(selfmember)-

                                              Extract a member from the archive as a file object. `member' may be a filename or a TarInfo object. If `member' is a regular file, a file-like object is returned. If `member' is a link, a file-like object is constructed from the link's target. If `member' is none of the above, None is returned. The file-like object is read-only and provides the following methods: read(), readline(), readlines(), seek() and tell()

  • makelink(selftarinfotargetpath)-

                                                                 Make a link called targetpath. If it cannot be created, we try to make a copy of the referenced file instead of a link.

  • next(self)-

                                                                 Return the next member of the archive as a TarInfo object, when TarFile is opened for reading. Return None if there is no more available.


import os
import tarfile

def hello(file):
    a = tarfile.open(file, mode="w")
    info = a.gettarinfo("tarfile.txt", arcname="changed.txt")
    print (a.getnames())