Globbing files by their creation date

Jean-Paul Calderone exarkun at divmod.com
Wed Jan 17 11:44:04 EST 2007


On 17 Jan 2007 08:31:07 -0800, tkpmep at hotmail.com wrote:
>
>Thanks a mill - os.path.getctime(f) is what I needed. Unfortunately, my
>attempts to turn the integer it returns into a date have failed.
>
>>>> os.path.getctime(fn)    #fn was created today, 1/17/2007
>1168955503
>
>I tried to convert this to a date object by typing
>>>>datetime.date.fromordinal(1168955503)
>
>Traceback (most recent call last):
>  File "<pyshell#9>", line 1, in -toplevel-
>    datetime.date.fromordinal(1168955503)
>ValueError: year is out of range
>
>How can I do the conversion? I'm trying to identify all files that were
>created after YYYY/MM/DD.
>
>For a quick sanity check, I ran
>>>> datetime.date.today().toordinal()
>732693
>
>which is orders of magnitude smaller than the number returned by
>os.path.getctime(fn).
>
>Thanks in advance for your help

Ordinals are unrelated to the values given back by getctime.  Try something
like this instead:

  exarkun at charm:~$ python
  Python 2.4.3 (#2, Oct  6 2006, 07:52:30)
  [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import os
  >>> import datetime
  >>> datetime.date.fromtimestamp(os.path.getctime('.'))
  datetime.date(2007, 1, 17)
  >>>
  exarkun at charm:~$ ls -ld .
  drwxr-xr-x 93 exarkun exarkun 8192 2007-01-17 10:34 .
  exarkun at charm:~$

Jean-Paul



More information about the Python-list mailing list