import arcpy, os #Set the workspace workspace = r"D:/Work" #create list of files from directories and subdirectories for root, dirs, files in arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon"): for name in files: if name.startswith("APE") or name.startswith("SAB"): path = os.path.abspath(os.path.join(root, name)) for x in path: outFC = x + "_pt" # convert to points arcpy.FeatureToPoint_management(path, outFC, "INSIDE")
Solved! Go to Solution.
import arcpy, os #Set the workspace in_workspace = r"D:\Projects" appendFC = r"C:\TEMP\PDX_SAB\PDX_SAB.gdb\PDX_SAB_Centerpoint" # make an in memory dataset its faster. no need to write to disk if you are just deleting it. outFC = "in_memory\\centerPoints" with open(r'D:/Projects/temp.txt','w',0) as f: # create list of files from directories and subdirectories for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="Polygon"): # disregard any folder named "ARCHIVE" in creating list if "ARCHIVE" in dirnames: dirnames.remove('ARCHIVE') for filename in filenames: if filename.startswith("APE") or filename.startswith("SAB"): print os.path.join(dirpath, filename) f.write(os.path.join(dirpath, filename)) f.write("\n") if arcpy.Exists(outFC): arcpy.Delete_management(outFC) # convert to points arcpy.FeatureToPoint_management(os.path.join(dirpath, filename), outFC, "INSIDE") # add field to capture project number arcpy.AddField_management(outFC, "ProjectNum", "TEXT", "", "", 20, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(outFC, "OriginalPath", "TEXT", "", "", 255, "", "NULLABLE", "REQUIRED") # extract ProjectNum from path and calc fields exp = str(os.path.join(dirpath, filename)[9:13]) exp2 = str(os.path.join(dirpath, filename) arcpy.CalculateField_management(outFC, "ProjectNum","exp", "PYTHON_9.3") arcpy.CalculateField_management(outFC, "OriginalPath","exp2" ,"PYTHON_9.3") # delete fields; use ListFields to get a list of field objects fieldObjList = arcpy.ListFields(outFC) # Create an empty list that will be populated with field names fieldNameList = [] # For each field in the object list, add the field name to the # name list. If the field is required, exclude it, to prevent errors for field in fieldObjList: if not field.required: fieldNameList.append(field.name) # delete field arcpy.DeleteField_management(outFC, fieldNameList) # append to final FC arcpy.Append_management(outFC, appendFC) f.write("done with script \n") f.close
import arcpy, os #Set the workspace workspace = r"D:/Work" appendFC = r"C:\TEMP\PDX_SAB\PDX_SAB.gdb\appendFC" outFC = r"\\in_memory\centerPoints" # make this an in memory dataset is faster. no need to write to disk if you are just deleting it. #create list of files from directories and subdirectories for root, dirs, files in arcpy.da.Walk(workspace, datatype="FeatureClass", type="Polygon"): for name in files: if name.startswith("APE") or name.startswith("SAB"): path = os.path.abspath(os.path.join(root, name)) for x in path: if "ARCHIVE" not in x: # empty the output feature class before adding new data if arcpy.Exists(outFC): arcpy.Delete_management(outFC) # if you are emptying it always, no need to check for size, just delete it if exists. # convert to points arcpy.FeatureToPoint_management(path, outFC, "INSIDE") # append to final FC arcpy.Append_management(outFC, appendFC)
import arcpy, os #Set the workspace workspace = r"D:/Work" appendFC = r"C:\TEMP\PDX_SAB\PDX_SAB.gdb\appendFC" outFC = r"\\in_memory\centerPoints" # make this an in memory dataset is faster. no need to write to disk if you are just deleting it. #create list of files from directories and subdirectories for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,topdown=True, datatype="FeatureClass", type="Polygon"): if "ARCHIVE" in dirnames: dirnames.remove('ARCHIVE') for filename in filenames: if filename.startswith("APE") or filename.startswith("SAB"): if arcpy.Exists(outFC): arcpy.Delete_management(outFC) # if you are emptying it always, no need to check for size, just delete it if exists. # convert to points arcpy.FeatureToPoint_management(os.path.join(dirpath,filename), outFC, "INSIDE") # append to final FC arcpy.Append_management(outFC, appendFC)
import arcpy, os #Set the workspace in_workspace = r"D:\Projects" appendFC = r"C:\TEMP\PDX_SAB\PDX_SAB.gdb\appendFC" # make an in memory dataset its faster. no need to write to disk if you are just deleting it. outFC = r"\\in_memory\centerPoints" #create list of files from directories and subdirectories for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="Polygon"): # disregard any folder named "ARCHIVE" in creating list if "ARCHIVE" in dirnames: dirnames.remove('ARCHIVE') for filename in filenames: if filename.startswith("APE") or filename.startswith("SAB"): if arcpy.Exists(outFC): arcpy.Delete_management(outFC) # convert to points arcpy.FeatureToPoint_management(os.path.join(dirpath, filename), outFC, "INSIDE") # append to final FC arcpy.Append_management(outFC, appendFC)
Thanks! I had just discovered the dirnames.remove feature. And yes, ARCHIVE is a subdirectory.
I am getting an error though: "ExecuteError: ERROR 000210: Cannot create output \\in_memory\centerPoints
Failed to execute (FeatureToPoint)."
Also, how do I get it to print (maybe to a text file?) the paths? I want to use that to make sure I'm only appending the ones I want, in case I need to add additional exceptions (like the ARCHIVE folder). Do I just put 'print filename' after the if filename startswith line?
outFC = "in_memory\\centerPoints"
with open('D:/update/temp.txt','a',0) as f: ### the "a" tells it to append to existing file. "w" for write would create a new file, even if it exists. print dt f.write(dt) f.write("\n\nthis is first line of image_tables.py \n\n") # Process: Delete if arcpy.Exists(Images_full): print "deleting ",Images_full f.write("\ndeleting ") f.write(Images_full) arcpy.Delete_management(Images_full, "Table") if arcpy.Exists(WasteSitePoly_centroids): print "deleting ",WasteSitePoly_centroids f.write("\ndeleting ") f.write(WasteSitePoly_centroids) arcpy.Delete_management(WasteSitePoly_centroids) f.close # at the same indentation level as the with open line
import arcpy, os #Set the workspace in_workspace = r"D:\Projects" appendFC = r"C:\TEMP\PDX_SAB\PDX_SAB.gdb\PDX_SAB_Centerpoint" # make an in memory dataset its faster. no need to write to disk if you are just deleting it. outFC = "in_memory\\centerPoints" # create list of files from directories and subdirectories for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="Polygon"): # disregard any folder named "ARCHIVE" in creating list if "ARCHIVE" in dirnames: dirnames.remove('ARCHIVE') for filename in filenames: if filename.startswith("APE") or filename.startswith("SAB"): print os.path.join(dirpath, filename) if arcpy.Exists(outFC): arcpy.Delete_management(outFC) # convert to points arcpy.FeatureToPoint_management(os.path.join(dirpath, filename), outFC, "INSIDE") # add field to capture project number arcpy.AddField_management(outFC, "ProjectNum", "TEXT", "", "", 20, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(outFC, "OriginalPath", "TEXT", "", "", 255, "", "NULLABLE", "REQUIRED") # extract ProjectNum from path and calc fields exp = str(os.path.join(dirpath, filename)[9:13]) arcpy.CalculateField_management(outFC, "ProjectNum", os.path.join(dirpath, filename), '"' + exp + '"', "PYTHON") arcpy.CalculateField_management(outFC, "OriginalPath", os.path.join(dirpath, filename), "PYTHON") # delete fields; use ListFields to get a list of field objects fieldObjList = arcpy.ListFields(outFC) # Create an empty list that will be populated with field names fieldNameList = [] # For each field in the object list, add the field name to the # name list. If the field is required, exclude it, to prevent errors for field in fieldObjList: if not field.required: fieldNameList.append(field.name) # delete field arcpy.DeleteField_management(outFC, fieldNameList) # append to final FC arcpy.Append_management(outFC, appendFC)
import arcpy, os #Set the workspace in_workspace = r"D:\Projects" appendFC = r"C:\TEMP\PDX_SAB\PDX_SAB.gdb\PDX_SAB_Centerpoint" # make an in memory dataset its faster. no need to write to disk if you are just deleting it. outFC = "in_memory\\centerPoints" with open(r'D:/Projects/temp.txt','w',0) as f: # create list of files from directories and subdirectories for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="Polygon"): # disregard any folder named "ARCHIVE" in creating list if "ARCHIVE" in dirnames: dirnames.remove('ARCHIVE') for filename in filenames: if filename.startswith("APE") or filename.startswith("SAB"): print os.path.join(dirpath, filename) f.write(os.path.join(dirpath, filename)) f.write("\n") if arcpy.Exists(outFC): arcpy.Delete_management(outFC) # convert to points arcpy.FeatureToPoint_management(os.path.join(dirpath, filename), outFC, "INSIDE") # add field to capture project number arcpy.AddField_management(outFC, "ProjectNum", "TEXT", "", "", 20, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(outFC, "OriginalPath", "TEXT", "", "", 255, "", "NULLABLE", "REQUIRED") # extract ProjectNum from path and calc fields exp = str(os.path.join(dirpath, filename)[9:13]) exp2 = str(os.path.join(dirpath, filename) arcpy.CalculateField_management(outFC, "ProjectNum","exp", "PYTHON_9.3") arcpy.CalculateField_management(outFC, "OriginalPath","exp2" ,"PYTHON_9.3") # delete fields; use ListFields to get a list of field objects fieldObjList = arcpy.ListFields(outFC) # Create an empty list that will be populated with field names fieldNameList = [] # For each field in the object list, add the field name to the # name list. If the field is required, exclude it, to prevent errors for field in fieldObjList: if not field.required: fieldNameList.append(field.name) # delete field arcpy.DeleteField_management(outFC, fieldNameList) # append to final FC arcpy.Append_management(outFC, appendFC) f.write("done with script \n") f.close
Can you tell me why you switched from "PYTHON" to "PYTHON_9.3" in the Calc Field expression? Is that so you can use "exp" without having to escape the quotation marks? From reading other posts I was under the impression that because exp and exp2 were strings they couldn't be used as variables without formatting them as '"' + exp + '"'.
â?¢To calculate strings to text or character fields, in the dialog box the string must be double-quoted ("string"), or in scripting, the double-quoted string must also be encapsulated in single quotes ('"string"').
exp2 = '"' + str(os.path.join(dirpath, filename)) + '"'
Even using model builder I still can't get the expression right to calc the path to a field. I tried the parse path but either I'm not using it right or that is not it's intention.
Change this:
exp2 = str(os.path.join(dirpath, filename)
to this:
exp2 = str(os.path.join(dirpath, filename))
Back in the python script I have a feeling the script is not running in the order I specified. I keep getting an error message about the schemas not matching to do the append. When I try printing the fields after the delete statement I see all the original fields - meaning no new fields were added and calc'd, and the old fields were not deleted.
How do I ensure that the processes run in the order I've put them in the script?
Specifies if the schema (field definitions) of the input datasets must match the schema of the target dataset in order for data to be appended.
â?¢TEST â??Input dataset schema (field definitions) must match the schema of the target dataset. An error will be returned if the schemas do not match.
â?¢NO_TEST â??Input dataset schema (field definitions) do not have to match that of the target dataset. Any fields from the input datasets that do not match the fields of the target dataset will not be mapped to the target dataset unless the mapping is explicitly set in the Field Map control.
arcpy.Append_management(outFC, appendFC, "NO_TEST", "", "")see if that gets past the schema issues. With the NO_TEST, it should copy the attributes that match exactly.
import arcpy, os #Set the workspace in_workspace = r"C:\TEMP\PDX_SAB" appendFC = r"C:\TEMP\PDX_SAB\PDX_SAB.gdb\PDX_SAB_Centerpoint" # make an in memory dataset its faster. no need to write to disk if you are just deleting it. outFC = "in_memory\\centerPoints" with open(r'C:\TEMP\PDX_SAB\paths.txt','w',0) as f: # create list of files from directories and subdirectories for dirpath, dirnames, filenames in arcpy.da.Walk(in_workspace, datatype="FeatureClass",type="Polygon"): # disregard any folder named "ARCHIVE" in creating list if "ARCHIVE" in dirnames: dirnames.remove('ARCHIVE') for filename in filenames: if filename.startswith("APE") or filename.startswith("SAB"): #if filename.name == "APE" or filename.name == "SAB": #print os.path.join(dirpath, filename) f.write(os.path.join(dirpath, filename)) f.write("\n") if arcpy.Exists(outFC): arcpy.Delete_management(outFC) # convert to points arcpy.FeatureToPoint_management(os.path.join(dirpath, filename), outFC, "INSIDE") # add field to capture project number arcpy.AddField_management(outFC, "ProjectNum", "TEXT", "", "", 20, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(outFC, "OriginalPath", "TEXT", "", "", 255, "", "NULLABLE", "REQUIRED") arcpy.AddField_management(outFC, "ServiceArea", "TEXT", "", "", 75, "", "NULLABLE", "REQUIRED") # set expressions to calculate fields exp = str(os.path.join(dirpath, filename)[16:21]) exp2 = str(os.path.join(dirpath, filename)) if filename.startswith("APE"): exp3 = "Cultural" else: exp3 = "Natural" arcpy.CalculateField_management(outFC, "ProjectNum", "exp", "PYTHON_9.3") arcpy.CalculateField_management(outFC, "OriginalPath", "exp2", "PYTHON_9.3") arcpy.CalculateField_management(outFC, "ServiceArea", "exp3", "PYTHON_9.3") # delete fields; use ListFields to get a list of field objects fieldObjList = arcpy.ListFields(outFC) # Create an empty list that will be populated with field names fieldNameList = [] # For each field in the object list, add the field name to the # name list. If the field is required, exclude it, to prevent errors for field in fieldObjList: if not field.required: #if field.name != "ProjectNum" and field.name != "OriginalPath": fieldNameList.append(field.name) # delete field arcpy.DeleteField_management(outFC, fieldNameList) # append to final FC arcpy.Append_management(outFC, appendFC) f.write("done with script \n") f.close