Compare File Save Date to a Specific Date

1297
5
01-28-2014 03:49 AM
MichaelVolz
Esteemed Contributor
I am working on a python script to process mxd files.  I would like to only process mxd files that are newer than a specific date.  I have been able to compare time from now to a specific date but it keeps changing from day to day.  I would actually just like to compare the date saved to a specific date.  Is this possible with some type of date comparison (date > some date)?
Tags (2)
0 Kudos
5 Replies
DouglasSands
Occasional Contributor II
Not knowing how you are getting the date that you are comparing to, for example if it is coded in the script or part of a tools input parameters, I'd say the easiest thing to do is get YYYY MM DD from the files and from your parameter. Then you could use the datetime library in python to do the comparisons. For example:

import datetime
filedate = datetime.date(2014, 1, 27)
comparedate = datetime.date(2013, 12, 31)
if (filedate - comparedate).days > 0:
    print('Work on this file')
else:
    print('Do nothing')


Would print

>>> Work on this file


The idea being that if you have a positive number of days between the file date and the compare date, then the file is 'new'. If the number of days is negative (or zero if the dates are the same), it is not new, so you wouldn't do anything.

Hope this helps
- Doug
0 Kudos
MichaelVolz
Esteemed Contributor
Do you know if this would work as I don't want to process files older than say 7/1/2011 which was the last time I batch processed mxd files (no one has modified these files in ~ 2.5 years)?

import datetime
filedate = datetime.fromtimestamp(os.path.getmtime(rootpath + "/" + fName))      #filedate = datetime.date(2014, 1, 27)
comparedate = datetime.now
if (filedate - comparedate).days > 915: #915 is ~ 2.5 years
    print('Work on this file')
else:
    print('Do nothing')

The problem here is that the time frame keeps getting longer as time goes by.  I want to compare the date of the file against a specific date so the time frame does not keep changing.
0 Kudos
MichaelVolz
Esteemed Contributor
I am having some difficulties getting something seemingly simple to work.

import datetime

then_test = datetime.date(2011, 7, 2)

print then_test

According to documentation this should print 2011-07-29

but I am getting the error message descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

Anyone know where I am going wrong here?
0 Kudos
DouglasSands
Occasional Contributor II
To your first question about comparing to a specific date:


import datetime
filedate = datetime.fromtimestamp(os.path.getmtime(rootpath + "/" + fName)) #filedate = datetime.date(2014, 1, 27)
today = datetime.now
maxdate = datetime.date(2011, 7, 2)
#Only do work if the number of days since the last change is less than the number of days since a 'too long ago' date.
if (filedate - today).days < (filedate - maxdate).days:
    print('Work on this file')
else:
    print('Do nothing')



When I try and test the 2nd issue you bring up, I don't see the same result as you:


>>> import datetime
>>> test = datetime.date(2011, 7, 2)
>>> print test
2011-07-02
>>> str(test)
'2011-07-02'
>>>


It works for me as would be expected, so I don't have a good answer for you regarding that issue.

- Doug
MichaelVolz
Esteemed Contributor
Doug:

Thanks for the assistance.

I finally found the issue which had to do with the import of the datetime module.

I had this statement at the top of my script

from datetime import datetime

maxdate = datetime.date(2011, 7, 2) throws the error I described previously


I had to change this statement to

maxdate = datetime(2011, 7, 2)

and the error disappeared


I did not realize I was using the additional module reference at the beginning of the script
0 Kudos