Reputation: 15
I don't think the title really says what my problem is but I don't know how to formulate it.
Anyway, I am printing from a log file all lines then I remember the last line in a variable. I then use a for loop to again go trough the whole file, but I use the fact that it has the time at the beginning of every line to check if the time is bigger then the one from the last line I saved last time I printed all the lines.
Now we get to the problem I have. It still prints the last line even if I don't want it too.
That might be confusing so let me use the output
['16', '31', '18']
[16:31:18] [Async Chat Thread - #121/INFO]: <seba_www> cat?
# the above line is the last line it printed the first time it went trough all the lines
# and I don't want to print it
['16', '31', '19']
[16:31:19] [Async Chat Thread - #121/INFO]: <seba_www> wdasdww
# the above line is the new line I want to be printed
I tried to fix it with a simple if statement:
if line != last_line:
print(line)
but it still prints it...
It might be really messy but I'm gonna put the whole block of code here as well, maybe it will help:
for line in logs:
print(line)
while True:
time.sleep(10)
last_line = line
s = last_line.split()
s1 = s[0]
if "[" in s1:
d2 = split(s1)
d = s1.find("[")
del d2[d]
d1 = s1.find("]")
del d2[d1 -1]
d2 = listToString(d2)
lastLine_list = d2.split(':')
logs = open(os.path.join(sys.path[0], 'latest1.log'))
for line in logs:
s = line.split()
s1 = s[0]
if "[" in s1:
d2 = split(s1)
d = s1.find("[")
del d2[d]
d1 = s1.find("]")
del d2[d1 - 1]
d2 = listToString(d2)
d2list = d2.split(":")
print(d2list)
if line != last_line:
if d2list[0] >= lastLine_list[0]:
if d2list[1] >= lastLine_list[1]:
if d2list[2] >= lastLine_list[2]:
print(line)
time.sleep(5)
logs.close()
Upvotes: 0
Views: 40
Reputation: 163
What happens if you try this line
if line != last_line:
print(line)
as
if last_line not in line:
print(line)
?
Upvotes: 1