Untar archive in Python with errors
<p>I download a bz2 file using Python. Then I want to unpack the archive using:</p>

<pre><code>def unpack_file(dir, file):
    cwd = os.getcwd()
    os.chdir(dir)
    print "Unpacking file %s" % file
    cmd = "tar -jxf %s" % file
    print cmd
    os.system(cmd)
    os.chdir(cwd)
</code></pre>

<p>Unfortunately this ends with error:</p>

<pre><code>bzip2: Compressed file ends unexpectedly;
    perhaps it is corrupted?  *Possible* reason follows.
bzip2: Inappropriate ioctl for device
    Input file = (stdin), output file = (stdout)

It is possible that the compressed file(s) have become corrupted.
You can use the -tvv option to test integrity of such files.

You can use the `bzip2recover' program to attempt to recover
data from undamaged sections of corrupted files.

tar: Nieoczekiwany EOF w archiwum
tar: Nieoczekiwany EOF w archiwum
tar: Error is not recoverable: exiting now
</code></pre>

<p>However I can unpack the archive from the shell without any problem.</p>

<p>Do you have any ideas what I do wrong?</p>
