deleting from tarfile

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Uwe Mayer

    deleting from tarfile

    Hi,

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

    Thanks
    Uwe
  • Mark McEahern

    #2
    Re: deleting from tarfile

    Uwe Mayer wrote:
    [color=blue]
    >Hi,
    >
    >is it possible to delete a file from a tar-archive using the tarfile module?
    >
    >Thanks
    >Uwe
    >
    >[/color]
    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(file name, nameToDelete):
    """Remove nameToDelete from tarfile filename."""
    prefix, ext = os.path.splitex t(filename)
    newFilename = '%(prefix)s-modified%(ext)s ' % locals()
    original = tarfile.open(fi lename)
    modified = tarfile.open(ne wFilename, 'w')
    for info in original.getmem bers():
    if info.name == nameToDelete:
    continue
    extracted = original.extrac tfile(info)
    if not extracted:
    continue
    modified.addfil e(info, extracted)
    original.close( )
    modified.close( )

    // m

    Comment

    • Michael Hoffman

      #3
      Re: deleting from tarfile

      Uwe Mayer wrote:[color=blue]
      > is it possible to delete a file from a tar-archive using the tarfile module?[/color]

      The tarlib.py in pyNMS claims to be able to do it. It doesn't use the
      tarfile module, though.

      Download pyNMS for free. This project has been renamed "pycopia", and extended. This is no longer maintained.

      --
      Michael Hoffman

      Comment

      • Colin J. Williams

        #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.

        Comment

        • Martin v. Löwis

          #5
          Re: deleting from tarfile

          Mark McEahern wrote:[color=blue]
          > It doesn't appear so. A workaround, of course, is to create a new file
          > with the subset of files from the old file:[/color]

          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

          Comment

          • Martin v. Löwis

            #6
            Re: deleting from tarfile

            Mark McEahern wrote:[color=blue]
            > It doesn't appear so. A workaround, of course, is to create a new file
            > with the subset of files from the old file:[/color]

            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

            Comment

            Working...