473,320 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

deleting from tarfile

Hi,

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

Thanks
Uwe
Jul 18 '05 #1
5 7437
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jay Donnell | last post by:
Is there a way to use the tarfile module to recursively compress the contents of a directory and maintain the directory structure in the tar archive? Simply doing os.system('tar -czvf ' +...
5
by: Matt Doucleff | last post by:
Hi everyone! I must be doing something wrong here :) I have a tarball that contains a single file whose contents are a pickled object. I would like to unpickle the object directly from the...
8
by: Dennis Hotson | last post by:
Hi, I'm trying to write a function that adds a file-like-object to a compressed tarfile... eg ".tar.gz" or ".tar.bz2" I've had a look at the tarfile module but the append mode doesn't support...
1
by: Beowulf | last post by:
Hello, I'm using Python to automate admin tasks on my job. We use Windoze 2000 as desktop platform. When executing this daily backup scripts I get the following error: Traceback (most recent...
1
by: Matthew Thorley | last post by:
I've been using tarfile like this import tarfile tar = tarfile.open('path_to_tar_archive', 'r:gz') But I need to use it like this: archive = open('path_to_tar_archive', 'r') tar =...
7
by: aurora00 | last post by:
I have a program that generates a number of files that will be packaged into a tarball. Can I stream the content into TarFile without first writing them out to the file system? All add(), addfile()...
6
by: sebastian.noack | last post by:
Hi, is there a way to or at least a reason why I can not use tarfile to create a gzip or bunzip2 compressed archive in the memory? You might might wanna answer "use StringIO" but this isn't...
6
by: Terry Carroll | last post by:
I am trying to do something with a very large tarfile from within Python, and am running into memory constraints. The tarfile in question is a 4-gigabyte datafile from freedb.org,...
1
by: boblatest | last post by:
Hello, I'm trying to catch an "EOFError" exception that occurs when reading truncated tarfile. Here's my routine, and below that the callback trace. Note that although I'm trying to catch all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.