Untar Python archive with errors

I am uploading a bz2 file using Python. Then I want to unzip the archive using:

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) 

Unfortunately, this ends with an error:

 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 

However, I can unzip the archive from the shell without any problems.

Do you have any idea what I'm doing wrong?

+4
source share
2 answers

For writing, the standard python library comes with a tarfile module that automatically processes the tar, tar.bz2, and tar.gz formats.

In addition, you can do great things, such as retrieving file lists, extracting subsets of files or directories, or a fragment of an archive so you can stream it (i.e. you don't need to unzip the entire file and then expand it. It does all in small pieces)

 import tarfile tar = tarfile.open("sample.tar.gz") tar.extractall() tar.close() 
+16
source

I would do it like this:

 import tarfile target_folder = '.' with tarfile.open("sample.tar.gz") as tar: tar.extractall(target_folder) 

What is it. tar / with takes care of the rest.

If you want to have a path to all files:

 import os filepaths = [] for (dirpath, dirnames, filenames) in walk(target_folder): filepaths.extend([os.path.join(dirpath, f) for f in filenames]) 
0
source

Source: https://habr.com/ru/post/1391412/


All Articles