Got Error when ran python - TypeError: can't compare datetime.datetime to datetime.date

2145
2
Jump to solution
02-24-2023 01:24 PM
AlexP_
by
Occasional Contributor III
Hello all,
 

I have ArcGIS Pro 3.0 and python 27. I am not expertise with code. Please see error and code below. It is working fine when using "processFilesAddedOnDate = datetime.strptime('01/01/2023', '%m/%d/%Y')". But when I switched to "processFilesAddedOnDate = date.today()", I got an error message. I am not sure what to do. Please kindly assist. Thank you so much in advance.

ERROR MESSAGE

= RESTART: M:\ArcGIS\ArcGIS\Projects\EC\PROD2023.py

Traceback (most recent call last):
File "M:\ArcGIS\ArcGIS\Projects\EC\PROD2023.py", line 33, in <module>
if datetime.fromtimestamp(os.path.getctime(os.path.join(root, f))) >= processFilesAddedOnDate:
TypeError: can't compare datetime.datetime to datetime.date
>>>

PYTHON SCRIPT CODE

 

 

import arcpy
import csv
import os
from datetime import date
from datetime import datetime

matchTableName = r"ElevationCertificatesAttachmentMatchTable.csv"
docFolder = r"Q:\2023\IMPORTED TO GIS"

matchField = "MatchID"
pathField = "Filename" 

#change this date to the disired value
#processFilesAddedOnDate = datetime.strptime('01/01/2023', '%m/%d/%Y')
processFilesAddedOnDate = date.today()

# iterate through each .pdf file in the directory and write a row to the table

       
for (root, dirs, files) in os.walk(docFolder):
    workingFolder = root
    matchTableNamePath = os.path.join(root, matchTableName)
    # create a new Match Table csv file
    if os.path.exists(matchTableNamePath):
        os.remove(matchTableNamePath)
    writer = csv.writer(open(matchTableNamePath, "w", newline=''), delimiter=",")
    # write a header row (the table will have two columns: ParcelID and Picture)
    writer.writerow([matchField, pathField, 'fullPath'])
    for f in files:
        
        if '.pdf' in f:
            #if datetime.strptime(os.path.getctime(os.path.join(root, f)),"%m/%d/%Y") >= processFilesAddedOnDate:
            if datetime.fromtimestamp(os.path.getctime(os.path.join(root, f))) >= processFilesAddedOnDate:
                
                #print(os.path.getctime(os.path.join(root, f)).strftime("%m/%d/%Y")+ '   ' + processFilesAddedOnDate.strftime("%m/%d/%Y"))
                fileNameToAddress = f.replace(".pdf", "")
                fileNameToAddress = fileNameToAddress.replace("  ", " ");
                fileNameToAddress = fileNameToAddress.replace("nw", "NW")
                try:
                    firstPartUntilNW = fileNameToAddress.split("NW")[0].strip().split(" ")
                    restAfterNW = fileNameToAddress.split("NW")[1].strip()
                    #print(firstPartUntilNW, len(firstPartUntilNW))
                    #print(restAfterNW)
                    fileNameToAddressWithNoPermitNumber = ""
                    if len(firstPartUntilNW) == 2:
                        fileNameToAddressWithNoPermitNumber = firstPartUntilNW[1]+" NW "+restAfterNW
                    if len(firstPartUntilNW)== 1:
                        fileNameToAddressWithNoPermitNumber = firstPartUntilNW[0]+" NW "+restAfterNW
                    #print(fileNameToAddressWithNoPermitNumber)
                    writer.writerow((fileNameToAddressWithNoPermitNumber,f, os.path.join(root, f)))
                except:
                    continue
      
    del writer

    # Set the workspace environment and run add attachment
    arcpy.env.workspace = r'C:\Users\AppData\Roaming\Esri\ArcGISPro\Favorites\test.sde'

    arcpy.management.AddAttachments('sde.123.test', 'ADDR', matchTableNamePath, "MatchID", "Filename", workingFolder)

 

 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

Well you're comparing a date to a datetime.
2023-02-01 00:00:00 >= 2023-01-01 00:00:00 - True
2023-02-01 00:00:00 >= 2023-01-01 - nope

Simplest would be to convert your datetime to a date or more efficient to get the date from the timestamp straightaway.
As I'm a simple person, psb:

 

if (datetime.fromtimestamp(os.path.getctime(os.path.join(root, f)))).date() >= processFilesAddedOnDate:

 

 

View solution in original post

2 Replies
DavidPike
MVP Frequent Contributor

Well you're comparing a date to a datetime.
2023-02-01 00:00:00 >= 2023-01-01 00:00:00 - True
2023-02-01 00:00:00 >= 2023-01-01 - nope

Simplest would be to convert your datetime to a date or more efficient to get the date from the timestamp straightaway.
As I'm a simple person, psb:

 

if (datetime.fromtimestamp(os.path.getctime(os.path.join(root, f)))).date() >= processFilesAddedOnDate:

 

 

MeGhanMWilliom
New Contributor

This error typically occurs when attempting to compare two different types of objects, such as an integer and a string. To resolve this issue, convert one of the objects to the same type as the other object before attempting to compare them. For example, if one object is an integer and the other is a string, you can convert the string to an integer using the int() function before attempting to compare them.