是否有合适的 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
会重置值?