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( )
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.
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.
Comment