我正在尝试在 python 中创建一个非常简单的日志解析器脚本。一切都按计划进行,除了目标机器上的脚本返回此错误(脚本在 unix 机器上工作,虽然很好):
for name in root.namelist():
Attribute Error: 'str' object has no attribute 'namelist'
Python 版本似乎相同(两台机器上都是 2.7.3)。有任何想法吗?
脚本本身:
import zipfile
import os
import re
string1 = "searchstring" # raw_input("usrinput: ")
try:
root = zipfile.ZipFile("/home/testuser/docs/testzip.zip", "r")
except:
root = "testfolder/"
for name in root.namelist():
if name.find(".") > 0:
f = root.open(name)
searchlines = f.readlines()
for i, line in enumerate(searchlines):
regex1 = "(.*)" + re.escape(string1) + "(.*)"
if re.match (regex1, line):
for l in searchlines[i-4:i+4]: print l,
print