423,688 Members | 2,147 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,688 IT Pros & Developers. It's quick & easy.

deleting from tarfile

P: n/a
Hi,

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

Thanks
Uwe
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
Uwe Mayer wrote:
is it possible to delete a file from a tar-archive using the tarfile module?


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

http://cvs.sourceforge.net/viewcvs.p....1.1&view=auto
--
Michael Hoffman
Jul 18 '05 #3

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

Colin W.
http://oss2005.case.unibz.it/
Jul 18 '05 #4

P: n/a
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
Jul 18 '05 #5

P: n/a
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
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.