Specifying a time from previous day
David Rushby
woodsplitter at rocketmail.com
Sun Apr 14 19:21:56 EDT 2002
"jrup" <jrup at mail.com> wrote:
> I need to search some log files, but only if they were created after 4:00pm
> the previous day.
>
> I've looked at the python time module and its documentation, and have
> skimmed mxDateTime documentation. I'm using activestate python 2.1.1 and
> would like to stay with it's native library since it's the one sanctioned by
> our senior nt person (also not sure how it would interact with the
> activestate install, or I'd try it).
>
> Can someone please point me in the right direction? Is this doable using the
> time module? I haven't had much luck figuring it out, yet.
It's certainly "doable" using the time module (see below), but
since:
a) the time module is rather low-level
b) mxDateTime is freely available, and high-level
c) you're a self-described newbie
, why confuse yourself by using the time module?
I see no reason why installing the eGenix.com mx Base Package would
harm an installation of Activestate Python.
An mxDateTime-based example appears below (with equivalent code
based on the time module also present, but commented out):
----------------------------------------------------------------
import glob, os
import mx.DateTime as dt
# import time
beginningOfToday = dt.today()
eightHours = dt.DateTimeDelta(0, 8)
# beginningOfToday = time.mktime(list(time.localtime()[:3])
# + [-1] + [0] * 5)
# eightHours = 60*60 * 8
minTime = beginningOfToday - eightHours
minTimeAsTicks = minTime.ticks()
print 'Will process all log files modified at or later than', minTime
# minTimeAsTicks = beginningOfToday - eightHours
# print 'Will process all log files modified at or later than',
# print time.asctime(time.localtime(minTimeAsTicks))
# logFilenamePattern could be input as a command-line
# argument using sys.argv
logFilenamePattern = r'c:\temp\*.txt'
logFilenames = glob.glob(logFilenamePattern)
# Build a list of relevant log filenames, including only the names of
# files that have been modified at or later than minTimeAsTicks.
relevantLogFilenames = [
logFilename
for logFilename in logFilenames
if os.stat(logFilename)[8] >= minTimeAsTicks
]
for logFilename in relevantLogFilenames:
logFile = open(logFilename)
print 'processing %s...' % logFilename
# Do some processing here.
logFile.close()
More information about the Python-list
mailing list