基本上我有这样一个文件系统:
main_archive.tar.gz
main_archive.tar
sub_archive.xml.gz
actual_file.xml
这个档案中有数百个文件。。。那么基本上
gzip
在Python3中,包可以与多个文件一起使用吗?我只对一个压缩文件使用过它,所以我不知道如何浏览多个文件或多个级别的“压缩”。
我通常的减压方法是:
with gzip.open(file_path, "rb") as f:
for ln in f.readlines():
*decode encoding here*
当然,这有多个问题,因为通常“f”只是一个文件。。。但现在我不确定它代表了什么?
任何帮助/建议都将不胜感激!
编辑1:
我已经接受了下面的答案,但如果你在寻找类似的代码,我的主干基本上是:
tar = tarfile.open(file_path, mode="r")
for member in tar.getmembers():
f = tar.extractfile(member)
if verbose:
print("Decoding", member.name, "...")
with gzip.open(f, "rb") as temp:
decoded = temp.read().decode("UTF-8")
e = xml.etree.ElementTree.parse(decoded).getroot()
for child in e:
print(child.tag)
print(child.attrib)
print("\n\n")
tar.close()
使用的主要包装是
gzip
,
tarfile
和
xml.etree.ElementTree
.