deleting from tarfile

U

Uwe Mayer

Hi,

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

Thanks
Uwe
 
M

Mark McEahern

Uwe said:
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
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Mark said:
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?=

Mark said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top