473,397 Members | 1,985 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,397 software developers and data experts.

problems with tarfile.open and tar.bz2

Hello everyone,

I need some tar functionality for my program. Currently the following
code properly displays tar archives, and tar.gz archives. However, it
chokes on tar.bz2 archives with the following error:

File "mail_filter.py", line 262, in extract_archive
tar_archive = tarfile.open('', 'r', fileobj)
File "/usr/lib/python2.3/tarfile.py", line 900, in open
return func(name, "r", fileobj)
File "/usr/lib/python2.3/tarfile.py", line 980, in bz2open
raise ValueError, "no support for external file objects"
ValueError: no support for external file objects
The code snippet is:

fileobj = StringIO(attach)

if attach_type == 'application/x-tgz' or attach_type ==
'application/x-tbz' or attach_type == 'application/x-tar':
print '%s file detected, attempting to decompress' %
(attach_type)

tar_archive = tarfile.open('', 'r', fileobj)

print tar_archive.getnames()
for archive_file in tar_archive.getmembers():
print archive_file
print tar_archive.extractfile(archive_file).read()
#except:
# print 'Error while extracting %s.' % (attach_type)
# return ''

I'm at my wits end with this error, it doesn't make any sense that it
would treat bzip2'd tar archives very differently than gz'd tar
archives.

Justin

Aug 26 '05 #1
3 2743

On 26 Aug 2005 09:05:29 -0700, ju**************@gmail.com said:
Hello everyone,

I need some tar functionality for my program. Currently the following
code properly displays tar archives, and tar.gz archives. However, it
chokes on tar.bz2 archives with the following error:

File "mail_filter.py", line 262, in extract_archive
tar_archive = tarfile.open('', 'r', fileobj)
File "/usr/lib/python2.3/tarfile.py", line 900, in open
return func(name, "r", fileobj)
File "/usr/lib/python2.3/tarfile.py", line 980, in bz2open
raise ValueError, "no support for external file objects"
ValueError: no support for external file objects
The code snippet is:

fileobj = StringIO(attach)

if attach_type == 'application/x-tgz' or attach_type ==
'application/x-tbz' or attach_type == 'application/x-tar':
print '%s file detected, attempting to decompress' %
(attach_type)

tar_archive = tarfile.open('', 'r', fileobj)

print tar_archive.getnames()
for archive_file in tar_archive.getmembers():
print archive_file
print tar_archive.extractfile(archive_file).read()
#except:
# print 'Error while extracting %s.' % (attach_type)
# return ''

Try opening with the 'r:bz2' mode instead of just 'r'.

Cheers,
Richard
Aug 26 '05 #2
r is supposed to autodetect the archive type.

However, even changing it to 'r:bz2' produces an identical error.

Aug 26 '05 #3
On Fri, 26 Aug 2005 09:05:29 -0700, justin.vanwinkle wrote:
Hello everyone,

I need some tar functionality for my program. Currently the following
code properly displays tar archives, and tar.gz archives. However, it
chokes on tar.bz2 archives with the following error:

File "mail_filter.py", line 262, in extract_archive
tar_archive = tarfile.open('', 'r', fileobj)
File "/usr/lib/python2.3/tarfile.py", line 900, in open
return func(name, "r", fileobj)
File "/usr/lib/python2.3/tarfile.py", line 980, in bz2open
raise ValueError, "no support for external file objects"
ValueError: no support for external file objects


The problem here is that the bz2.BZ2File class that tarfile.py uses to
access tar.bz2 files has no support for an external file-object argument.
In contrast to gzip.GzipFile, it works solely on real files.
This limitation is somewhat annoying, but so far no one took the trouble
changing the bz2 module.

If your program does not rely on random access to the tar.bz2 file, you
can still use the "r|bz2" stream mode:

tar_archive = tarfile.open(mode="r|bz2", fileobj=StringIO.StringIO(attach))
for tarinfo in tar_archive:
print tarinfo.name
print tar_archive.extractfile(tarinfo).read()
tar_archive.close()

--
Lars Gustäbel
la**@gustaebel.de

Aug 28 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Hans-Joachim Widmaier | last post by:
Although I've done a bit of ranting before on this, noone seems to have noticed. I'll try again, hopefully more to the point. One of the additions in the standard library I liked most is the...
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 =...
3
by: Anurag | last post by:
Hi, I am trying to use tarfile module to list contents of a 'gz' file but it seems to hang for large files and CPU usage goes 100%. though 'tar -tvf' on same file list contents in couple of...
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.