Creating a TAR File : tar file « File « Python Tutorial






File Modes for Python's tarfile Module

Mode  Description
r      Opens a TAR file for reading. 
r:      Opens a TAR file for reading with no compression.
w or w:  Opens a TAR file for writing with no compression.
a or a:  Opens a TAR file for appending with no compression.
r:gz  Opens a TAR file for reading with gzip compression.
w:gz  Opens a TAR file for writing with gzip compression.
r:bz2  Opens a TAR file for reading with bzip2 compression.
w:bz2  Opens a TAR file for writing with bzip2 compression.

import os
import tarfile

#Create Tar file
tFile = tarfile.open("files.tar", 'w')

files = os.listdir(".")
for f in files:
    tFile.add(f)

#List files in tar
for f in tFile.getnames():
    print "Added %s" % f

tFile.close()








12.22.tar file
12.22.1.Creating a TAR File
12.22.2.Extracting a File from a TAR File