Solved! Go to Solution.
Hi,
I have a script that writes .dbf files to a folder. The first time the script runs it does what it is supposed to. The second time I run it however, it says the first .dbf to be created already exists and stops. It's not overwriting the files.
I have arcpy.env.overwriteOutput = True and had it successfully working to overwrite at one point in the development of the script. I'm not sure what made it stop working. I'm pretty sure I added the field mapping and the listing of the files in the log file after it was running smoothly but, have commented them out and it still gives me the error message. Any suggestions would be appreciated....I have no idea what is causing the problem.# Import arcpy module import arcpy, sys, traceback, time, datetime, os # Set overwrite option arcpy.env.overwriteOutput = True # INPUT NEEDED: Set Read me file workspace CURDATE = datetime.date.today() logpath = "X:\\Script\Out\ReadMe" logfile1 = logpath + str (" ") + str(CURDATE) + ".txt" if arcpy.Exists(logfile1): arcpy.Delete_management(logfile1) log1 = open(logfile1, "a") print "Generate Readme file " + str(CURDATE) print >> log1, time.strftime("%c") print >> log1, "End Processing " #Set the current workspace for input file geodatabase arcpy.env.workspace = "X:\\Script\Input.gdb" #Set Local variables specifyting the output table folder: outTable = "X:\\Script\Out" print "Start Processing" #Set a Search Cursor on the Polygons srows = arcpy.SearchCursor("Polygons", "","", "WATERSHED", "" ) srow = srows.next() print "Get Polygon" mapFields = ["ID", "STUDY", "NAME"] fieldMappings = arcpy.FieldMappings() for mapField in mapFields: fieldMap = arcpy.FieldMap() fieldMap.addInputField("Points", mapField) fieldMappings.addFieldMap(fieldMap) while srow: pid = srow.ObjectID poly_lyr = arcpy.MakeFeatureLayer_management("Polygons",'pgon_select',""" "ObjectID" = """+ str(pid)) print "Watershed is " + str(srow.WATERSHED) arcpy.MakeFeatureLayer_management("Points", "Points_lyr", "", "", ) print "Make point feature layer" arcpy.SelectLayerByLocation_management("Points_lyr", "INTERSECT", "pgon_select", "", "NEW_SELECTION") print "Select points by " + str(srow.WATERSHED) + " watershed" arcpy.TableToTable_conversion("Points_lyr", outTable , str(srow.WATERSHED)+ ".dbf","") print "Make table " + str(srow.WATERSHED) srow = srows.next() print "Next Polygon" print "Write .dbf tables to log file" print >> log1, "Files generated = " file_list = os.listdir(outTable) for file in file_list: if file.endswith (".dbf"): log1.write(file + "\n") print "End Processing" log1.close()
del srows
#Set a Search Cursor on the Polygons srows = arcpy.SearchCursor("Polygons", "","", "WATERSHED", "" ) sheds= list() for srow in srows: sheds.append(srow.WATERSHED) del row,srows # close/delete cursor variables for shed in sheds: ... arcpy.MakeFeaturelayer_management('"WATERSHED" = \'' + shed + "'"...) ...
Hi,
I have a script that writes .dbf files to a folder. The first time the script runs it does what it is supposed to. The second time I run it however, it says the first .dbf to be created already exists and stops. It's not overwriting the files.
I have arcpy.env.overwriteOutput = True and had it successfully working to overwrite at one point in the development of the script. I'm not sure what made it stop working. I'm pretty sure I added the field mapping and the listing of the files in the log file after it was running smoothly but, have commented them out and it still gives me the error message. Any suggestions would be appreciated....I have no idea what is causing the problem.# Import arcpy module import arcpy, sys, traceback, time, datetime, os # Set overwrite option arcpy.env.overwriteOutput = True # INPUT NEEDED: Set Read me file workspace CURDATE = datetime.date.today() logpath = "X:\\Script\Out\ReadMe" logfile1 = logpath + str (" ") + str(CURDATE) + ".txt" if arcpy.Exists(logfile1): arcpy.Delete_management(logfile1) log1 = open(logfile1, "a") print "Generate Readme file " + str(CURDATE) print >> log1, time.strftime("%c") print >> log1, "End Processing " #Set the current workspace for input file geodatabase arcpy.env.workspace = "X:\\Script\Input.gdb" #Set Local variables specifyting the output table folder: outTable = "X:\\Script\Out" print "Start Processing" #Set a Search Cursor on the Polygons srows = arcpy.SearchCursor("Polygons", "","", "WATERSHED", "" ) srow = srows.next() print "Get Polygon" mapFields = ["ID", "STUDY", "NAME"] fieldMappings = arcpy.FieldMappings() for mapField in mapFields: fieldMap = arcpy.FieldMap() fieldMap.addInputField("Points", mapField) fieldMappings.addFieldMap(fieldMap) while srow: pid = srow.ObjectID poly_lyr = arcpy.MakeFeatureLayer_management("Polygons",'pgon_select',""" "ObjectID" = """+ str(pid)) print "Watershed is " + str(srow.WATERSHED) arcpy.MakeFeatureLayer_management("Points", "Points_lyr", "", "", ) print "Make point feature layer" arcpy.SelectLayerByLocation_management("Points_lyr", "INTERSECT", "pgon_select", "", "NEW_SELECTION") print "Select points by " + str(srow.WATERSHED) + " watershed" arcpy.TableToTable_conversion("Points_lyr", outTable , str(srow.WATERSHED)+ ".dbf","") print "Make table " + str(srow.WATERSHED) srow = srows.next() print "Next Polygon" print "Write .dbf tables to log file" print >> log1, "Files generated = " file_list = os.listdir(outTable) for file in file_list: if file.endswith (".dbf"): log1.write(file + "\n") print "End Processing" log1.close()
del srows
#Set a Search Cursor on the Polygons srows = arcpy.SearchCursor("Polygons", "","", "WATERSHED", "" ) sheds= list() for srow in srows: sheds.append(srow.WATERSHED) del row,srows # close/delete cursor variables for shed in sheds: ... arcpy.MakeFeaturelayer_management('"WATERSHED" = \'' + shed + "'"...) ...
mdenil,
Although the Zen is enlightening, the overwrite function still won't work as needed. Is there a specific piece of code you could suggest? I'm a python novice.
tabName = srow.WATERSHED + ".dbf") tabPath = os.path.join(outTable,tabName) if arcpy.exists(tabPath): arcpy.Delete_management(tabPath) arcpy.TableToTable_conversion("Points_lyr", outTable , tabName)
if arcpy.Exists()for every layer.
try: <code> except: <code>and it worked. Why that makes all the difference is beyond me but it might be worth trying because it worked for me.
I don't know if it's a python issue or an ESRI/arcpy issue and I don't know why, but this last tidbit of info (try:except) solved a problem I've been having off and on for months. Halle-freaking-lujah ! Thank you.
try: lyr1 = arcpy.MakeFeatureLayer("myfile.shp","myLyr") # ... do stuff with lyr 1 except: # ... do stuff you want to do if it fails finally: # do stuff you want to do no matter what, for example, try to delete the layer: try: arcpy.Delete_management(lyr1) except: pass