2

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?

3
  • I recommend investigating arcpy.mapping (in particular ListLayers) and arcpy.da.Walk both here and in the ArcGIS Help.
    – PolyGeo
    Commented Jan 25, 2015 at 12:05
  • 1
    Which version of ArcGIS are you using? Commented Jan 25, 2015 at 17:39
  • I'm using arcview 10.3
    – newGIS
    Commented Jan 26, 2015 at 6:00

1 Answer 1

2

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
6
  • i get an error: UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in position 18: invalid continuation byte
    – newGIS
    Commented 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
    Commented 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 please
    – newGIS
    Commented Jan 27, 2015 at 8:03
  • Please try latter code..
    – Learner
    Commented 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
    Commented Jan 27, 2015 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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