Select to view content in your preferred language

If-then statement/true-false condition

5462
4
Jump to solution
01-09-2013 07:03 AM
by Anonymous User
Not applicable
Original User: MCline19

I'm having a problem with my second If-then statement.  It seems like it should be a simple fix, but I'm new to coding so I'm struggling to figure out why I have my first and second If-then statements set up the same and the first works, but the second doesn't. 

For the second If-then statement, whether the feature compare condition is true or false, the if-then statement always runs it as though true and therefore never executes my "else". 

Any advise is greatly apprecaited.

import arcpy import os import datetime from datetime import date, timedelta from arcpy import env env.workspace = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports' import smtplib import string  # Scheduler. Determines the day of the week.  Program only checks for new road additions on weekdays. print ("Finding current date and time...") now = datetime.datetime.now() dayofweek = datetime.datetime.now().weekday() print now if dayofweek == 0:  print ("Monday") elif dayofweek == 1:  print ("Tuesday") elif dayofweek == 2:  print ("Wednesday") elif dayofweek == 3:  print ("Thursday") elif dayofweek == 4:  print ("Friday") else:  print ("End: Not programmed to run on weekends.")  #Export public_works.PW.Streets to shapefile and name according to the date todaysdate = str(datetime.date.today()) todaysdate = todaysdate.replace('-', '_') print todaysdate if dayofweek in [0, 1, 2, 3, 4]:  arcpy.CopyFeatures_management(r'G:\Department Projects\Public Works\Public Works.sde\public_works.PW.Streets', r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + todaysdate + '.shp') else:  print ("End: Not programmed to run on weekends (2).")  #Feature compare.  Compare yesterdays file with Todays new file. yesterday = str(date.today() - timedelta(1)) yesterday = yesterday.replace('-', '_') #yesterday = "2013_01_07" print yesterday todaysfile = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + todaysdate + '.shp' yesterfile = r'G:\GISAdmin\NEW_ROADS_NOTIFICATION\Road_Exports\roads' + yesterday + '.shp' print todaysfile print yesterfile  if arcpy.FeatureCompare_management(yesterfile, todaysfile, "FID"):  print ("No changes detected in " + todaysfile + ". No further processing necessary.") else:  print ("Sending email...")  SUBJECT = "Steets layer changed detected"  TO = "[email protected]"  FROM = "[email protected]"  text = "Breaking News!  A change has been detected in the Public Works street spatial database."  BODY = string.join((   "From: %s" % FROM,   "To: %s" % TO,   "Subject: %s" % SUBJECT ,   "",   text   ), "\r\n")  server = smtplib.SMTP(HOST)  server.sendmail(FROM, [TO], BODY)  server.quit()  print ("Email sent!")
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Original User: curtvprice

if arcpy.FeatureCompare_management(yesterfile, todaysfile, "FID"):


The above code (the third if, BTW) will not work because the tool returns a result object, not a boolean. You need to unpack the result object to get the boolean out. From the help for the tool:

The comparison tools result object will be 'true' when no differences are found and 'false' when differences are detected.

View solution in original post

0 Kudos
4 Replies
MathewCoyle
Honored Contributor
Which line are you having a problem with? Looking at your email else statement you have nothing assigned to your HOST variable so it won't work.
0 Kudos
by Anonymous User
Not applicable
Original User: curtvprice

if arcpy.FeatureCompare_management(yesterfile, todaysfile, "FID"):


The above code (the third if, BTW) will not work because the tool returns a result object, not a boolean. You need to unpack the result object to get the boolean out. From the help for the tool:

The comparison tools result object will be 'true' when no differences are found and 'false' when differences are detected.
0 Kudos
by Anonymous User
Not applicable
Original User: MCline19

Specifically: Lines 47-50.  I am aware the host email variable is missing (waiting to receive that info from another individual).  I've been omitting that portion of the code when I run it - just trying to get the "Sending email" print statement to print for a confirmation that it is going through the correct channels.  But whether the feature compare condition is true or false, the if-then statement always runs it as though true and never executes my "else" print statement.
0 Kudos
by Anonymous User
Not applicable
Original User: MCline19

That makes sense.  Thanks Curtis.
0 Kudos