Can't extract .xz files with python "tarfile.ReadError: file could not be opened successfully"
Can't extract .xz files with python "tarfile.ReadError: file could not be opened successfully"
我需要使用 python 提取一些压缩为 .xz 文件的文本文件。
我的代码就是
import tarfile
tarfile.open('file.xz')
但这失败并出现错误
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
我已经在很多 .xz 文件上尝试过这个并得到了相同的结果。 .xz 文件没有损坏,可以使用 gnome 存档管理器正常打开。
我搜索了问题并找到了 this bug report,但我不确定现在该尝试什么。
如果不是.tar.xz
文件,而是.xz
文件,需要使用lzma
module,而不是tarfile
模块:
import lzma
with lzma.open("file.xz") as f:
file_content = f.read()
要保存提取的内容:
with lzma.open("file.xz") as f, open('extracted', 'wb') as fout:
file_content = f.read()
fout.write(file_content)
我需要使用 python 提取一些压缩为 .xz 文件的文本文件。
我的代码就是
import tarfile
tarfile.open('file.xz')
但这失败并出现错误
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
我已经在很多 .xz 文件上尝试过这个并得到了相同的结果。 .xz 文件没有损坏,可以使用 gnome 存档管理器正常打开。
我搜索了问题并找到了 this bug report,但我不确定现在该尝试什么。
如果不是.tar.xz
文件,而是.xz
文件,需要使用lzma
module,而不是tarfile
模块:
import lzma
with lzma.open("file.xz") as f:
file_content = f.read()
要保存提取的内容:
with lzma.open("file.xz") as f, open('extracted', 'wb') as fout:
file_content = f.read()
fout.write(file_content)