Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Globbing files by their creation date

1 view
Skip to first unread message

tkp...@hotmail.com

unread,
Jan 16, 2007, 8:34:14 AM1/16/07
to
I'd like to create a list of all files in a directory that were created
after a certain date. How does one do this? I've used glob.glob to
create a list of all files whose name matches a substring, but I don't
see how I can use it to identify files by their creation date.

Thanks in advance for the assistance.

Thomas Philips

sk...@pobox.com

unread,
Jan 16, 2007, 8:44:29 AM1/16/07
to tkp...@hotmail.com, pytho...@python.org

Thomas> I've used glob.glob to create a list of all files whose name
Thomas> matches a substring, but I don't see how I can use it to
Thomas> identify files by their creation date.

Sumthin' like:

files = [f for f in glob.glob(globpat)
if os.path.getctime(f) > timethreshold]

Define globpat and timethreshold accordingly. You sure you don't mean
modification time? If so, change getctime to getmtime.

Skip

James Antill

unread,
Jan 16, 2007, 3:33:52 PM1/16/07
to

help(os.path.getctime)

getctime(filename)
Return the metadata change time of a file, reported by os.stat().

Note that _change_ time != creation time. Also for all files you probably
want os.listdir() instead of glob.

--
James Antill -- ja...@and.org
http://www.and.org/and-httpd/ -- $2,000 security guarantee
http://www.and.org/vstr/

tkp...@hotmail.com

unread,
Jan 17, 2007, 11:31:07 AM1/17/07
to

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

Thomas Philips

Jean-Paul Calderone

unread,
Jan 17, 2007, 11:44:04 AM1/17/07
to pytho...@python.org

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

exarkun@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@charm:~$ ls -ld .
drwxr-xr-x 93 exarkun exarkun 8192 2007-01-17 10:34 .
exarkun@charm:~$

Jean-Paul

0 new messages