2

I need to extract some text files compressed to .xz files using python.

My code is just

import tarfile 

tarfile.open('file.xz')

But this fails with the error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/tarfile.py", line 1558, in open
    raise ReadError("file could not be opened successfully")
tarfile.ReadError: file could not be opened successfully

I have tried this on many .xz files and got the same result. The .xz files are not broken and can be opened fine with gnome archive manager.

I searched the problem and found this bug report but I'm not sure what to try now.

falsetru
  • 357,413
  • 63
  • 732
  • 636
Qwertie
  • 5,784
  • 12
  • 45
  • 89
  • Shouldn't it be: `tarfile.open('file.xz')` – hjpotter92 Nov 15 '15 at 09:26
  • 1
    Is a `.tar.xz` or simply a `.xz` file? – falsetru Nov 15 '15 at 09:28
  • @hjpotter92 Yes it should be. My actual code had the '' – Qwertie Nov 15 '15 at 09:28
  • @falsetru Its just .xz – Qwertie Nov 15 '15 at 09:28
  • I had a similar issue and didn't find any answers on the internet, so I untar'd and retar'd the file, and that worked around the issue. – mmacvicar Jun 27 '16 at 20:02
  • Possible duplicate of [How to unpack xz file with python which contains only data but no filename?](https://stackoverflow.com/questions/42079724/how-to-unpack-xz-file-with-python-which-contains-only-data-but-no-filename) – Doktor J Jun 12 '19 at 16:36
  • @DoktorJ Thats not the same question and it was asked years later. – Qwertie Jun 13 '19 at 00:13
  • @Qwertie both questions are about opening a `.xz` file with `tarfile`, and the answer is the same -- except the answer on the other one also covers Python 2.x's `backports.lzma`. Both questions explain that the files open fine in other programs, and give the same error when attempting to open with `tarfile`. Additionally, the other comes up first in searches and has more upvotes on both Q and A. Chronological order is irrelevant when flagging a dupe; I learned that one the hard way myself. – Doktor J Jun 13 '19 at 04:55

1 Answers1

5

If it's not a .tar.xz file, but a .xz file, you need to use lzma module, not tarfile module:

import lzma

with lzma.open("file.xz") as f:
    file_content = f.read()

To save the extracted content:

with lzma.open("file.xz") as f, open('extracted', 'wb') as fout:
    file_content = f.read()
    fout.write(file_content)
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Ah, I see. I got it mixed up because [this question](https://stackoverflow.com/questions/17217073/how-to-decompress-a-xz-file-which-has-multiple-folders-files-inside-in-a-singl) uses tarfile but I see they where using tar.xz – Qwertie Nov 15 '15 at 09:31
  • @Qwertie, Ah.. another answer by me ;) – falsetru Nov 15 '15 at 09:32
  • Just one last question. How would I save the file after extracting because extractall('.') no longer works. – Qwertie Nov 15 '15 at 09:36
  • @Qwertie, I updated the answer to include it: Open file with "write" mode, and write the content there. – falsetru Nov 15 '15 at 09:38