1

Overall objective: Open up an MXD object, copy an SDE feature class to a local GDB, then run a validation check on the data. Check is to see if there's a front and back photo for each marker point (if there is, some do not have secondary photos).

I want to output the results to a text file. I've tried altering where the line breaks happen, but nothing is written aside from the "validation started at" line and the line meant to be a header "OBJECT_ID....MARKER_ID....Missing Photo Name". I added "txtFile.close()" even though I thought 'with' statements were supposed to handle that on cleanup. Any idea why my output isn't being written to a text file?

My script:

##PART ONE###

import os, arcpy, time, sys, datetime, timeit
from time import clock, strftime, gmtime
from arcpy import env

#Start Timer
start = time.clock()

mxd = arcpy.mapping.MapDocument(r"C:\TempGIS\PhotoCheck\PHOTO_CHECK.mxd")

arcpy.env.overwriteOutput = True

out = r'C:\TempGIS\PhotoCheck\PhotoCheck_SDE_Copy.gdb\Dataset1'

nm = "MARKER_PT"

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
        ds = lyr.dataSource
        nm = "MARKER_PT"
        print str(nm) + " is being copied"
        arcpy.FeatureClassToFeatureClass_conversion(ds, out, nm)
        print str(nm) + " has been copied to the standalone GDB."

###PART TWO###

print "Checking " + str(nm) + " ...."

nm = out + "\\" + "MARKER_PT"
uCur = arcpy.UpdateCursor(nm)
hs = uCur.next()

#Next Section
while hs:
    OID = str(hs.getValue("OBJECTID"))
    gsID = str(hs.getValue("MARKER_ID"))
    siteID = str(hs.getValue("SITE_ID"))
    fPhoto = str(hs.getValue("FRONT_IMG"))
    rPhoto = str(hs.getValue("REAR_IMG"))
    fPhotoURL = str(hs.getValue("FRONT_IMG_URL"))
    rPhotoURL = str(hs.getValue("REAR_IMG_URL"))
    Active = str(hs.getValue("ACTIVE"))
    with open(r"C:\TempGIS\PhotoCheck\MissingPhotosList.txt", "w") as txtFile:
        txtFile.write("This validation started at: " + str(datetime.datetime.now().date()) + '\n')
        txtFile.write("OBJECT_ID" + "       " + "MARKER_ID" + "         "+ "MISSING PHOTO NAME" + '\n')
        if Active == '1':
            try:
                if len(fPhoto) > 4 and len(fPhotoURL) > 4:
                    if os.path.isfile(fPhotoURL) is False:
                        txtFile.write(OID + "               " + gsID + "            " + fPhoto + '\n')
                        print str(fPhoto) + " is missing!"
                if len(rPhoto) > 4 and len(rPhotoURL) > 4:
                    if os.path.isfile(rPhotoURL) is False:  
                        txtFile.write(OID + "               " + gsID + "            " + rPhoto + '\n')
                        print str(rPhoto) + " is missing!"
            except:
                "Looks like all photos are accounted for!"
        hs = uCur.next()

del uCur, mxd, nm, hs, out

#Stop Timer
end = time.clock()
elapsed = end - start
print "~~~~~ ***** Photo Validation script has finished! ***** ~~~~~"
print "Time taken to finish this script (Hrs:Min:Sec)---> " + strftime("%H:"+"%M:"+"%S", gmtime(elapsed))

#End Script

Printing to the IDLE window works perfectly and I've found the ones listed are actually missing, but my text file remains empty except for the first two lines.

3

You are opening the file in Write (w) mode within the feature loop. In this mode, if the file already exists, it is erased. You should move the file opening command before the hs loop.

Should the last processed record have an Active value different than 1 and nothing is printed in this last file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.