是否有适当的Pythonic方法来确定文件是否在一周的特定日期(或几天)的特定时间之间创建 - 例如周一至周五09:00至17:00?
目前我有:
def IsEligible(filename, between = None, weekdays = None):
"""
Determines the elgibility of a file for processing based on
the date and time it was modified (mtime). 'weekdays' is a list
of day numbers (Monday = 0 .. Sunday = 6) or None to indicate ALL
days of the week. 'between' is a tuple which contains the
lower and upper limits (as datetime.time objects) of the periods
under consideration or None to indicate ALL times of day.
"""
modified = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
dow = modified.weekday()
mtime = modified.time()
if between is not None:
earliest = min(between)
latest = max(between)
if mtime < earliest or mtime >= latest:
return False
if weekdays is not None and dow not in weekdays:
return False
print(filename, modified)
return True
工作正常,但我不知道是否有更聪明的东西。 (我知道它有点罗嗦,但希望它的可读性更强)。
最后一件事,我最初使用ctime
而不是mtime
,但它没有产生我所追求的结果,而且似乎只是为了返回当前时间,即使文件没有&自创建以来被修改过的任何东西。在什么情况下ctime
值会重置?