I am trying to read a zipped file in python. I want to read only files with “debug” in their names and only print lines which have BROKER_LOGON in them. It somehow does not read line by line, but prints the entire file which has BROKER_LOGON in it. Please tell me if there is a way to read line by line from a zipped file.
import os
import zipfile
import re
def main():
try:
root = zipfile.ZipFile("C:/Documents and Settings/Desktop/20110526-1708-server.zip", "r")
except:
root = "."
for name in root.namelist():
i = name.find("debug")
if i>0:
line = root.read(name).find("BROKER_LOGON")
if line >0:
print line
if __name__== "__main__":
main()
This code reads the raw file contents with root.read(name), splits them to lines and then scans the lines.