Navigation

This Page

Examples

The output from all the example programs from PyMOTW has been
generated with Python 2.7.4, unless otherwise noted. Some
of the features described here may not be available in earlier
versions of Python.

Navigation

The tarfile module provides read and write access to UNIX tar
archives, including compressed files. In addition to the POSIX
standards, several GNU tar extensions are supported. Various UNIX
special file types (hard and soft links, device nodes, etc.) are also
handled.

If you know in advance the name of the archive member, you can
retrieve its TarInfo object with getmember().

importtarfileimporttimet=tarfile.open('example.tar','r')forfilenamein['README.txt','notthere.txt']:try:info=t.getmember(filename)exceptKeyError:print'ERROR: Did not find %s in tar archive'%filenameelse:print'%s is %d bytes'%(info.name,info.size)

To access the data from an archive member within your program, use the
extractfile() method, passing the member’s name.

importtarfilet=tarfile.open('example.tar','r')forfilenamein['README.txt','notthere.txt']:try:f=t.extractfile(filename)exceptKeyError:print'ERROR: Did not find %s in tar archive'%filenameelse:printfilename,':',f.read()

$ python tarfile_extractfile.py
README.txt : The examples for the tarfile module use this file and example.tar as data.
ERROR: Did not find notthere.txt in tar archive

If you just want to unpack the archive and write the files to the
filesystem, use extract() or extractall() instead.

Sometimes you want to write data to an archive but the data is not in
a file on the filesystem. Rather than writing the data to a file, then
adding that file to the archive, you can use addfile() to add
data from an open file-like handle.

importtarfilefromcStringIOimportStringIOdata='This is the data to write to the archive.'out=tarfile.open('tarfile_addfile_string.tar',mode='w')try:info=tarfile.TarInfo('made_up_file.txt')info.size=len(data)out.addfile(info,StringIO(data))finally:out.close()printprint'Contents:'t=tarfile.open('tarfile_addfile_string.tar','r')formember_infoint.getmembers():printmember_info.namef=t.extractfile(member_info)printf.read()

By first constructing a TarInfo object ourselves, we can give
the archive member any name we wish. After setting the size, we can
write the data to the archive using addfile() and passing a
StringIO buffer as a source of the data.

$ python tarfile_addfile_string.py
Contents:
made_up_file.txt
This is the data to write to the archive.

Besides regular tar archive files, the tarfile module can work
with archives compressed via the gzip or bzip2 protocols. To open a
compressed archive, modify the mode string passed to open() to include
":gz" or ":bz2", depending on the compression method you want
to use.