[Tutor] get last 7 days string in a log file

spawgi at gmail.com spawgi at gmail.com
Mon Aug 1 20:24:53 CEST 2011


Let me understand the problem
- Do you want to count the number of times the application was restarted
between today and 7 days back?
=> If yes, then you will need to do some string manipulation to get the
required dates. Then you will need to apply both the conditions to the line
i.e. if the date is the required date and if the string contains what you
i.e. if "2009-03-6" in line and <similar condition for the start text>:
... count += 1
You can apply this logic to each date and create a complex statement using
OR to get the count.
OR you can use the datetime object with proper formatting and check the
existence of the condition.

Do I understand your problem correctly?


2011/8/1 Bjørn-Roar Eriksen <mediacus at gmail.com>

> Hi
> I'm new to Python and I'm trying to write a script that's count restart of
> an application. The log file contains:
> 2009-03-06 18:20:26,423  User operation - start nanny
> 2009-03-06 18:20:26,423  User operation - start all services
> And 2009-03-06 18:20:26,423  User operation - start all services tells me
> that the application has been restarted.
> My script do count it, but I cant figure out how to code so it's only get
> from to day and 7 day's back.
> #Counts restart of an application
> count = 0
> with open("c:/test/bre.log")as f:
>     for line in f:
>         if "User operation - start all services" in line:
>             count += 1
>             #print line.strip()
>     print 'Server restart: %s' % count
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

We can do it and do it better.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110801/126b9b28/attachment.html>

More information about the Tutor mailing list