Continuously to Merge shapefiles in folders and subfolders with arcpy I try to edit attribute table for 20 shapefiles that spread in big a folder and are divided to a lot of sub folders. All shapefiles are called "migrashim" and the name of the filed that i want to change is "structuretype". I try this code:
import arcpy,os,sys,string,fnmatch,arcpy.mapping
from arcpy import env
rootPath = r"C:\Project\layers"
pattern = 'migrashim.shp'
features = arcpy.UpdateCursor(r"C:\Project\layers\*.shp")
counter = 0
for root, dirs, files in os.walk(rootPath):
for filename in files:
if filename == "migrashim.shp":
for feature in features:
if feature.structuretype == 3:
feature.structuretype = 4
features.updateRow(feature)
counter = counter + 1
print 'edit'
print counter
del feature,features
but i got an error:
IOError: "C:\Project\layers\migrashim.shp" does not exist
What wrong with my code?
-
I recommend investigating arcpy.mapping (in particular ListLayers) and arcpy.da.Walk both here and in the ArcGIS Help.PolyGeo– PolyGeo ♦2015年01月25日 12:05:43 +00:00Commented Jan 25, 2015 at 12:05
-
1Which version of ArcGIS are you using?Emil Brundage– Emil Brundage2015年01月25日 17:39:37 +00:00Commented Jan 25, 2015 at 17:39
-
I'm using arcview 10.3newGIS– newGIS2015年01月26日 06:00:02 +00:00Commented Jan 26, 2015 at 6:00
1 Answer 1
I would use below code--
If you have access to "Data Access Module"
import arcpy,os,sys
pattern = 'fish_46.shp'
folder = 'C:\Users\USER_NAME\Desktop\delete'## root folder
field = 'Id' ## your field where calculation to be applied
files_process = []
for root,dirs,files in os.walk('C:\Users\USER_NAME\Desktop\delete'):
for filenames in files:
if filenames == pattern:
files_process.append(os.path.join(root, filenames))
for i in files_process:
curU = arcpy.da.UpdateCursor(i,field)
for row in curU:
row[0]=1 ##Any expression you want
curU.updateRow(row)
del curU
EDIT
If you do not have access to "Data Access Module"
import arcpy,os,sys
pattern = 'fish_46.shp'
folder = 'C:\Users\USER_NAME\Desktop\delete'## root folder
field = 'Id' ## your field where calculation to be applied
files_process = []
for root,dirs,files in os.walk('C:\Users\USER_NAME\Desktop\delete'):
for filenames in files:
if filenames == pattern:
files_process.append(os.path.join(root, filenames))
for i in files_process:
curU = arcpy.UpdateCursor(i)
for row in curU:
row.setValue(field, 500)##Any expression you want
curU.updateRow(row)
del curU
-
i get an error: UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in position 18: invalid continuation bytenewGIS– newGIS2015年01月26日 13:49:44 +00:00Commented Jan 26, 2015 at 13:49
-
I checked the code right now and it's working perfectly.Please confirm there is no non-ASCII chars in the scope this script gets through and please do not cut-copy-paste code into the IDE from ms-word, notepad etc. I found some times those text processors convert simple inverted commas into bizarre non-unicode/ascii chars and again i am to type them manually.Learner– Learner2015年01月27日 05:04:32 +00:00Commented Jan 27, 2015 at 5:04
-
i get an error: File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects\mixins.py", line 16, in <module> curU = arcpy.da.UpdateCursor(i,field) AttributeError: 'module' object has no attribute 'da' i need help pleasenewGIS– newGIS2015年01月27日 08:03:54 +00:00Commented Jan 27, 2015 at 8:03
-
Please try latter code..Learner– Learner2015年01月27日 08:37:44 +00:00Commented Jan 27, 2015 at 8:37
-
Use the later added, i mean second block, i mean lowest code after the word EDIT in my answer....................Learner– Learner2015年01月27日 11:33:16 +00:00Commented Jan 27, 2015 at 11:33
Explore related questions
See similar questions with these tags.