[英]How to read .rtf file and convert into python3 strings and can be stored in python3 list?
[英]How to read and list a tgz file in python3?
在python 3(3.6.8)中,我想读取gzip压缩的tar文件并列出其内容。
我找到了产生错误的解决方案
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte
在搜索此错误时发现了此建议,因此我尝试了以下代码段:
with open(out_file) as fd:
gzip_fd = gzip.GzipFile(fileobj=fd)
tar = tarfile.open(gzip_fd.read())
产生相同的错误!
那么怎么做对呢?
即使在查看实际文档时,我也想出了以下代码:
tar = tarfile.open(out_file, "w:gz")
for member in tar.getnames():
print(tar.extractfile(member).read())
最终没有任何错误-但没有在屏幕上打印tar存档的任何内容!
tar文件格式正确,包含文件夹和文件。 (我需要尝试共享此文件)
当您open
文件而不指定mode
,默认情况mode
将其读取为文本。 您需要使用mode='rb'
标志将文件作为原始字节流打开,然后将其送入gzip阅读器
with open(out_file, mode='rb') as fd:
gzip_fd = gzip.GzipFile(fileobj=fd)
tar = tarfile.open(gzip_fd.read())
python-archive模块(可在pip上获得)可以帮助您:
from archive import extract
file = "you/file.tgz"
try:
extract(file, "out/%s.raw" % (file), ext=".tgz")
except:
# could not extract
pass
可用的扩展名是(v0.2):'.zip','。egg','。jar','。tar','。tar.gz','。tgz','。tar.bz2','。 tz2'
不知道为什么以前不起作用,但是以下解决方案对我有用,以便使用python 3.6列出gzip压缩tar存档的文件和文件夹 :
tar = tarfile.open(filename, "r:gz")
print(tar.getnames())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.