boaz_shuster
boaz_shuster

Reputation: 2925

Python tarfile doesn't untar

I have the following file:

# ls -lha
total 2.4M
drwxr-xr-x. 2 root     root     4.0K Nov 26 19:47 .
drwxrwxr-x. 5 bshuster bshuster 4.0K Nov 26 19:46 ..
-rw-r--r--. 1 root     root     2.4M Nov 26 19:26 logs.tar.bz2

untarring the file

#  tar -xvjf logs.tar.bz2
/var/log/
/var/log/btmp
/var/log/messages

however, when doing it through tarfile module in Python, it doesn't do much.

# rm -rf var && python
Python 2.7.5 (default, Nov  3 2014, 14:26:24) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tarfile
>>> import os
>>> tar = tarfile.open("logs.tar.bz2")
>>> tar.extractall('.')
>>> os.listdir('.')
['logs.tar.bz2']
>>> tar.close()
>>> os.listdir('.')
['logs.tar.bz2']

what am I doing wrong?

Best Regards.

Upvotes: 1

Views: 169

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49813

You need to specify that bz2 compression is being used:

tar = tarfile.open("logs.tar.bz2", "r:bz2")

Upvotes: 4

Related Questions