deleting from tarfile

Discussion in 'Python' started by Uwe Mayer, Jan 15, 2005.

  1. Uwe Mayer

    Uwe Mayer Guest

    Hi,

    is it possible to delete a file from a tar-archive using the tarfile module?

    Thanks
    Uwe
    Uwe Mayer, Jan 15, 2005
    #1
    1. Advertising

  2. Uwe Mayer wrote:

    >Hi,
    >
    >is it possible to delete a file from a tar-archive using the tarfile module?
    >
    >Thanks
    >Uwe
    >
    >

    It doesn't appear so. A workaround, of course, is to create a new file
    with the subset of files from the old file:

    #!/usr/bin/env python

    import tarfile
    import os

    def removeFile(filename, nameToDelete):
    """Remove nameToDelete from tarfile filename."""
    prefix, ext = os.path.splitext(filename)
    newFilename = '%(prefix)s-modified%(ext)s' % locals()
    original = tarfile.open(filename)
    modified = tarfile.open(newFilename, 'w')
    for info in original.getmembers():
    if info.name == nameToDelete:
    continue
    extracted = original.extractfile(info)
    if not extracted:
    continue
    modified.addfile(info, extracted)
    original.close()
    modified.close()

    // m
    Mark McEahern, Jan 16, 2005
    #2
    1. Advertising

  3. Michael Hoffman, Jan 16, 2005
    #3
  4. The First International Conference on Open Source Systems - OSS 2005- Main

    I haven't spotted a posting on c.l.p but someone here may be interested.

    Colin W.
    http://oss2005.case.unibz.it/
    Colin J. Williams, Jan 16, 2005
    #4
  5. Mark McEahern wrote:
    > It doesn't appear so. A workaround, of course, is to create a new file
    > with the subset of files from the old file:


    That is actually the *only* way to do that. tarfiles cannot be "sparse",
    in the sense that parts of the file can be marked as deleted. So in
    order to delete a file, you have to copy the entire tarfile, and skip
    the file you want to delete - whether you do this yourself, or whether
    tarfile.py does it for you.

    Regards,
    Martin
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Jan 16, 2005
    #5
  6. Mark McEahern wrote:
    > It doesn't appear so. A workaround, of course, is to create a new file
    > with the subset of files from the old file:


    That is actually the *only* way to do that. tarfiles cannot be "sparse",
    in the sense that parts of the file can be marked as deleted. So in
    order to delete a file, you have to copy the entire tarfile, and skip
    the file you want to delete - whether you do this yourself, or whether
    tarfile.py does it for you.

    Regards,
    Martin
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Jan 16, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Josh Smith

    tarfile and end-of-tape

    Josh Smith, Aug 4, 2003, in forum: Python
    Replies:
    1
    Views:
    316
    Lars Gustaebel
    Aug 5, 2003
  2. Marc Poinot (Onera)

    TarFile using binary strings

    Marc Poinot (Onera), Aug 12, 2003, in forum: Python
    Replies:
    0
    Views:
    332
    Marc Poinot (Onera)
    Aug 12, 2003
  3. Lars Behrens
    Replies:
    4
    Views:
    432
    Lars Gustaebel
    Aug 20, 2003
  4. Harry Barker
    Replies:
    2
    Views:
    514
    Alf P. Steinbach
    Apr 19, 2006
  5. crea
    Replies:
    2
    Views:
    409
    Nobody
    Dec 28, 2012
Loading...

Share This Page