date and time comparison how to

MRAB python at mrabarnett.plus.com
Mon Oct 29 20:50:48 EDT 2012


On 2012-10-30 00:04, Gary Herron wrote:
> On 10/29/2012 04:13 PM, noydb wrote:
>> All,
>>
>> I need help with a date and time comparison.
>>
>> Say a user enters a date-n-time and a file on disk.  I want to compare the date and time of the file to the entered date-n-time; if the file is newer than the entered date-n-time, add the file to a list to process.
>>
>> How best to do?  I have looked at the datetime module, tried a few things, no luck.
>>
>> Is os.stat a part of it?  Tried, not sure of the output, the st_mtime/st_ctime doesnt jive with the file's correct date and time.  ??
>>
>> Any help would be appreciated!
>
> Use the datetime module (distributed with Python) to compare date/times.
>
> You can turn a filesystem time into a datetime with something like the
> following:
>                   import datetime, os, stat
>                   mtime = os.lstat(filename)[stat.ST_MTIME]   // the
> files modification time
>                   dt = datetime.datetime.fromtimestamp(mtime)
>
>
Instead of os.lstat(filename)[stat.ST_MTIME] you could use
os.path.getmtime(filename).



More information about the Python-list mailing list