problems with tarfile.open and tar.bz2

Richard Lewis richardlewis at fastmail.co.uk
Fri Aug 26 12:13:56 EDT 2005


On 26 Aug 2005 09:05:29 -0700, justin.vanwinkle at 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



More information about the Python-list mailing list