generator - Python:AttributeError:“生成器”对象没有“读取”属性
问题描述
我尝试使用此类存档。但是,我从“return Archive('r',strm.read())”中得到了“AttributeError: 'generator' object has no attribute 'read'”。
class Archive(object):
def __init__(self, mode, data=None):
self.file = io.BytesIO(data)
self.tar = tarfile.open(mode=mode, fileobj=self.file)
def add_text_file(self, filename: str, text: str, encoding='utf-8'):
"""Add the contents of `text` to a new entry in the `tar`
file.
:return:
self
"""
b = text.encode(encoding)
f = io.BytesIO(b)
info = tarfile.TarInfo(filename)
info.size = len(b)
info.type = tarfile.REGTYPE
info.mtime = time.time()
self.tar.addfile(info, fileobj=f)
return self
def get_text_file(self, filename: str, encoding='utf-8') -> str:
"""Read the contents of a file in the archive.
:return:
contents of file in string.
"""
f = self.tar.extractfile(filename)
if f:
return f.read().decode(encoding)
return None
def close(self):
self.tar.close()
return self
def __iter__(self):
return iter(self.tar.getmembers())
@property
def buffer(self):
return self.file.getvalue()
主要问题:
def get_archive(self, path):
"""Get a file or directory from the container and make it into
an `Archive` object."""
if self.working_dir is not None and not posixpath.isabs(path):
path = posixpath.join(self.working_dir, path)
strm, stat = self.client.get_archive(
self.container_id, path)
return Archive('r', strm.read())
有人可以告诉我如何读取或将原始 tar 数据流的 strm 转换为适合归档类的字节类对象吗?或任何其他想法?
解决方案
当我在新的 Python 虚拟环境中运行并重新安装所有必需的软件包时,该问题已得到解决。但是,我仍然不明白它是如何工作的。
推荐阅读
- python-3.x - 读取 CSV 以及如何将 CSV 文件的名称写入 CSV 的每一行
- django - Django - 如果使用过滤器,如何更改视图中的默认查询集?
- python - 为什么数据框和系列之间的运算符“*”给我一个带有 NaN 的 len(firstSeries)*len(secondSeries)?
- php - 我的注册表单在 localhost 中运行良好,但是当我将它移动到服务器时它只是刷新并且没有任何反应。可能是什么问题
- r - 使用 guides() 更改标题图例位置,而不将图例分解为离散数字
- sql-server - 有没有办法在不发布的情况下导出 dacpac 的版本?
- javascript - 尽管推断了模型,但无法为 face-api.js 加载模型
- file - 用lua读取文件txt
- javascript - 如何在javascript中创建屏幕保护程序
- mongodb - 如何在 Meteor 中发布/订阅外部 MongoDB