繁体   English   中英

“int”类型的参数不可迭代 - 写入另一个文件时出现 Python 错误

[英]argument of type 'int' is not iterable - Python error while writing to another file

我正在打开一个文件并检查该文件中是否包含“Error”关键字。 如果找到,我将错误内容作为附件写为 email 发送。

with open(self.filePath, 'rb') as content_file:
    content = content_file.readline()
content = [i for i in content if "Error" in i]
content = "\n".join(content)
self.msg.add_attachment(content, maintype='text', subtype='plain', filename=self.filePath.split('/')[-1])

但是我收到一个错误

argument of type 'int' is not iterable

请告诉我如何处理这个问题。 我尝试添加b"Error" ,但这也引发了错误。

您需要readlines而不是readline或者遍历文件,如下所示:

with open(self.filePath, 'rb') as content_file:
    content = [i for i in content_file if b"Error" in i]
content = b"\n".join(content)

迭代bytes对象会得到int对象,如果in运算符没有__contains__魔术方法,则返回迭代其第二个参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM