Find Files in a Folder Between 2 Dates

Tim Chase python.list at
Fri Dec 5 00:42:19 CET 2008

> Is there a way to find all the files in a folder, between 2 dates?
> For example:
> Firstdate = 200801010000
> Seconddate = 200801020000
> Find all the files in C:\Folder that are between Firstdate and
> SecondDate.

This should do it:

   import time
   import os
   firstdate = "200801010000"
   seconddate = "200801020000"
   def get_timestamp(s):
     return time.mktime(time.strptime(s, "%Y%m%d%H%M"))
   start = get_timestamp(firstdate)
   end = get_timestamp(seconddate)
   location = '/path/to/wherever/'
   files = [fname
     for fname in os.listdir(location)
     if start <=
       os.stat(os.path.join(location, fname)).st_mtime
       <= end
   print files

The magic is the the os.stat(f).st_mtime to determine when the 
associated timestamp, the os.listdir() to get the available files 
in a directory, and the list-comprehension to filter to only the 
ones you want.


More information about the Python-list mailing list