Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

If-then statement/true-false condition

$
0
0
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.

Code:

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 = "python@mydomain.com"
        FROM = "python@mydomain.com"
        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!")


Viewing all articles
Browse latest Browse all 2485