【发布时间】:2020-08-07 09:15:28
【问题描述】:
我在 Python 中传递 tgz 文件的类文件对象时遇到问题。这是我的代码的样子:
backup = tarfile.open(backup_file, mode='r:gz')
for f in backup.getmembers():
if f.name.endswith('.xml'):
ff = f.name
backupff = backup.extractfile(ff)
if backupff:
backupobj = backupff.read()
backup.close()
问题出在
backupobj = backupff.read()
它给出了这个错误:
AttributeError: 'bytes' 对象没有属性 'read'
我在处理 zip 文件时没有这样的问题。
更新
@AKX,你说得对,这不是我正在运行的代码。真正的代码非常大,我不确定是否有人有时间研究它。
无论如何,当我运行 main 函数时,我收到此错误:
file_read = file.read
AttributeError: 'bytes' 对象没有属性 'read'
这里是 file.read 部分:
def sendfile(self, file, offset=0, count=None):
"""Borrowed from https://github.com/python/cpython/blob/3.6/Lib/socket.py
and adapted to our needs
"""
self._check_sendfile_params(file, offset, count)
if self.request.gettimeout() == 0:
raise ValueError("non-blocking sockets are not supported")
if offset:
file.seek(offset)
blocksize = min(count, 8192) if count else 8192
total_sent = 0
# localize variable access to minimize overhead
file_read = file.read
sock_send = self.request.send
【问题讨论】: