can tarfile maintain directory structure?

Jeff Epler jepler at unpythonic.net
Tue Aug 17 22:06:45 EDT 2004


You can use os.walk (or os.path.walk for older versions of Python) to
recurse a directory tree.  Here's a simple script to use tarfile and
os.walk:

    import tarfile, sys, os

    t = tarfile.TarFile(sys.argv[1], "w")
    for f in sys.argv[2:]:
        for dirpath, dirnames, filenames in os.walk(f):
            for f in filenames:
                f = os.path.join(dirpath, f)
                print "Adding", f
                t.add(f)

    t.close()

Here's a sample session with it:
  * Creating a simple directory structure
    $ mkdir a
    $ touch a/file.txt
    $ mkdir a/subdir
    $ touch a/subdir/subfile.txt

  * Invoking the script
    $ python ~/mktar.py test.tar a
    Adding a/file.txt
    Adding a/subdir/subfile .txt

  * Checking on the results
    $ tar tvf test.tar
    -rw-rw-r-- jepler/jepler     0 2004-08-17 21:00:57 a/file.txt
    -rw-rw-r-- jepler/jepler     0 2004-08-17 21:01:03 a/subdir/subfile.txt

I suspect that to get compressed output would involve use of gzip.open
and the 3-argument TarFile constructor, something like
    import gzip
    g = gzip.open(sys.argv[1], "w")
    t = tarfile.TarFile(sys.argv[1], "w", g)
    ...
indeed, this seems to work for me.
    $ python ~/mktargz.py test.tar.gz a
    Adding a/file.txt
    Adding a/subdir/subfile.txt
    $ file test.tar.gz
    test.tar.gz: gzip compressed data, was "test.tar", max compression

Jeff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20040817/32da967e/attachment.sig>


More information about the Python-list mailing list