python tarfile module (compressed and pressurized tar, bz2)

The TarFile class is an instance of the tar package

It is composed of member blocks, and the member block includes header block and data block. Each member is described in the form of TarInfo object. Therefore, TarFile is the sequence of TarInfo. The formal parameters of its initialization function correspond to the attributes of TarFile class, of which the more important is the dereference attribute. The default is false (at this time, the symbolic file will be saved as a symbol file), When set to True, its target file is saved in a compressed package

  • getmember(name), getmembers(), getnames(): return the TarInfo of the specified file name, all TarInfo and all TarInfo file names respectively. The latter two are in the same order. If there are many members with the same name, the last one will be taken
  • list(verbose=True)   List the file information in tar. Verbose is relative to ls -l
  • next()   It is called by the for method iterator and returns the next TarInfo object
  • extractall(path = ",", members=None), extract all files,   Path is the decompression path. The default is the current directory. If members (a subset, TarInfo list, or iterator) is specified
  • extract(member, path = ""), extract the file of the specified member, and path is the decompression path
  • Extract file (member): extract the corresponding object as a read-only file object   Member can be the file name or TarInfo
  • add(name, arcname=None, recursive=True, exclude=None, filter=None): create a TarInfo object according to the file name and add the file to the compressed package. You can specify another name used by arcname in the compressed package,   Recursive is whether the folder is processed recursively,   Exclude is not recommended. Filter (in the form of key=value) is a function name, the input is a TarInfo object, and a new TarInfo object or none is returned (none will not be written to the compressed package and can be used for filtering, so exclude is replaced)
  • addfile(tarinfo, fileobj=None): add the TarInfo object or file object to the compressed package. It is generally used with gettarinfo
  • gettarinfo(name=None, arcname=None, fileobj=None): create the TarInfo object through the file name or file object. Arcname can rename the file

Compressed instance

import tarfile

# compress
tar = tarfile.open('your.tar','w')  # Create a compressed package
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log')  # Add the file to the package and name it
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log')  #
tar.close()  # Close the package

# decompression
tar = tarfile.open('your.tar','r')  # Open a compressed package
tar.extractall()  # Unzip all files in the package (you can set the unzip address)
tar.close()  # Close the package

Compress all files in a directory

def compress_file(tarfilename, dirname):    # tarfilename is the name of the compressed package, and dirname is the directory to package
    if os.path.isfile(dirname):
        with tarfile.open(tarfilename, 'w') as tar:
            tar.add(dirname)
    else:
        with tarfile.open(tarfilename, 'w') as tar:
            for root, dirs, files in os.walk(dirname):
                for single_file in files:
                    # if single_file != tarfilename:
                    filepath = os.path.join(root, single_file)
                    tar.add(filepath)

compress_file('test.tar', 'test.txt')
compress_file('t.tar', '.')

Add files to the existing tar package

def addfile(tarfilename, dirname):    # tarfilename is the name of the compressed package, and dirname is the directory to package
    if os.path.isfile(dirname):
        with tarfile.open(tarfilename, 'a') as tar:
            tar.add(dirname)
    else:
        with tarfile.open(tarfilename, 'a') as tar:
            for root, dirs, files in os.walk(dirname):
                for single_file in files:
                    # if single_file != tarfilename:
                    filepath = os.path.join(root, single_file)
                    tar.add(filepath)

addfile('t.tar', 'ttt.txt')
addfile('t.tar', 'ttt')

mode value

'r' or 'r:*'   Open for reading with transparent compression (recommended).
'r:'   Open for reading exclusively without compression.
'r:gz'   Open for reading with gzip compression.
'r:bz2'   Open for reading with bzip2 compression.
'a' or 'a:'   Open for appending with no compression. The file is created if it does not exist.
'w' or 'w:'   Open for uncompressed writing.
'w:gz'   Open for gzip compressed writing.
'w:bz2'   Open for bzip2 compressed writing.

Keywords: Python

Added by ONiX on Fri, 01 Oct 2021 01:35:09 +0300