Website hosting service by

 Back to Index

7.19.3 Examples

How to create an uncompressed tar archive from a list of filenames:

import tarfile
tar ="sample.tar", "w")
for name in ["foo", "bar", "quux"]:

How to read a gzip compressed tar archive and display some member information:

import tarfile
tar ="sample.tar.gz", "r:gz")
for tarinfo in tar:
    print, "is", tarinfo.size, "bytes in size and is",
    if tarinfo.isreg():
        print "a regular file."
    elif tarinfo.isdir():
        print "a directory."
        print "something else."

How to create a tar archive with faked information:

import tarfile
tar ="sample.tar.gz", "w:gz")
for name in namelist:
    tarinfo = tar.gettarinfo(name, "fakeproj-1.0/" + name)
    tarinfo.uid = 123
    tarinfo.gid = 456
    tarinfo.uname = "johndoe"
    tarinfo.gname = "fake"
    tar.addfile(tarinfo, file(name))

The only way to extract an uncompressed tar stream from sys.stdin:

import sys
import tarfile
tar ="r|", fileobj=sys.stdin)
for tarinfo in tar:




2002-2004 Webhosting Service


Disclaimer: This documentation is provided only for the benefits of our hosting customers.
For authoritative source of the documentation, please refer to offers domain name registration, domain name transfer and domain search services  

Cheap domain registration : Register domain name or buy domain name, including free domain hosting services


Domain registration : Buy domain name or register domain name from $5.95/year only