我的 python 程序通过 ssh 获取邮件日志数据。当我尝试每行遍历它时
with text as f:
for line in f:
try:
.... regex stuff....
我得到错误:
Traceback (most recent call last):
File "xxxxxxxxxxxxxxxxxxxxxxxx", line 90, in <module>
start()
File "xxxxxxxxxxxxxxxxxxxxxxxx", line 64, in start
with text as f:
AttributeError: __exit__
那不起作用,唯一对我有用的解决方案是以下。当我将文本保存为文件并再次打开它时。但是该文件大约有 1.24 MB 大,这会不必要地减慢程序的速度。有谁知道我怎样才能摆脱额外的储蓄?
....
stdin, stdout, stderr = ssh.exec_command('sudo cat /var/log/mailing.log')
text = ''.join(stdout.readlines())
text_file = open('mail.log', 'w')
text_file.write(text)
text_file.close()
ssh.close()
with open('mail.log') as f:
for line in f:
try:
.... regex stuff....