-1

I am trying to use my Python script for Merge in ArcGIS as a tool. (to use this script in model)

But it occurs an error (below figure).

I don't know what problem is...

  1. Python script : work in ArcGIS geoprocessing-python

    import sys, arcpy, os
     reload(sys)
     from collections import defaultdict as dd
    
     arcpy.env.workspace = r"C:\test2"
     folder = arcpy.env.workspace
     out_folder = r"C:\test1"
    
    
     sys.setdefaultencoding('utf-8')
    
    
     d = dd(list)
     for root, folder, files in os.walk(folder):
         for file in files:
             if os.path.isfile(os.path.join(root, file)) and file.endswith('.shp'):
            ##    print(file)
                 d[file[:4]].append(os.path.join(root, file))
    
     #Execute code above then print(d) to make sure its ok before merging
     for group, mergelist in d.items():
         #print(group, mergelist)
         arcpy.Merge_management(inputs=mergelist, output=os.path.join(out_folder, '{}.shp'.format(group)))
    

2. Revised Python script for the custom tool : error

  import sys, os
reload(sys)
from collections import defaultdict as dd
import arcpy
from arcpy import env


inWorkspace = arcpy.GetParameterAsText(0)
outWorkspace = arcpy.GetParameterAsText(1)
env.workspace = inWorkspace

sys.setdefaultencoding('utf-8')


d = dd(list)
   for root, inWorkspace, files in os.walk(inWorkspace):
      for file in files:
         if os.path.isfile(os.path.join(root, file)) and 
                  file.endswith('.shp'):
   ##    print(file)
        d[file[:4]].append(os.path.join(root, file))

  #Execute code above then print(d) to make sure its ok before merging
      for group, mergelist in d.items():
  #print(group, mergelist)
        arcpy.Merge_management(inputs=mergelist, 
      output=os.path.join(outWorkspace, '{}0.shp'.format(group)))

enter image description here

enter image description here

1 Answer 1

2

You have an IndentationError so this is really a Python rather than ArcPy question.

The code below should not throw an IndentationError but I have not examined it against what you are trying to do, so I suggest you use it to just get past where you are currently stuck:

import sys, os
reload(sys)
from collections import defaultdict as dd
import arcpy
from arcpy import env

inWorkspace = arcpy.GetParameterAsText(0)
outWorkspace = arcpy.GetParameterAsText(1)
env.workspace = inWorkspace

sys.setdefaultencoding('utf-8')

d = dd(list)
for root, inWorkspace, files in os.walk(inWorkspace):
    for file in files:
        if os.path.isfile(os.path.join(root, file)) and file.endswith('.shp'):
            ##    print(file)
            d[file[:4]].append(os.path.join(root, file))
#Execute code above then print(d) to make sure its ok before merging
for group, mergelist in d.items():
    #print(group, mergelist)
    arcpy.Merge_management(inputs=mergelist,
                           output=os.path.join(
                               outWorkspace, '{}0.shp'.format(group)))
0

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.