1

I am calling a list of names in from a text file and would like to use each of those to make new layers. I am running into an issue where I get "TypeError: cannot concatenate 'str' and 'list' objects" and am not sure how to get past this. I tried a couple of different where clauses without much luck.

*EDIT: Working code below. The issue was I am calling the file directly in the walk and the list fields. so when I am appending the path I needed to remove the filename from os.path.join(dirpath, filename).

import arcpy, os
from arcpy import env

#setup input varible
ws = r"M:/"

cl = r"/Admin_1.shp"

dem = r"M:/SRTM/dem"

#set output as a variable
out = r"M:/Elevation_Masks/Masks"
    with open('M:/Elevation_Masks/test_space/elevCountries_Test.txt', 'r') as f:
    cb = [line.strip() for line in f]

CNTRY_NAME_FIELD = 'CNTRY_NAME'

for dirpath, dirnames, filenames in arcpy.da.Walk(cl, datatype="FeatureClass", type="ALL"):
    for filename in filenames:
        if CNTRY_NAME in [f.name for f in arcpy.ListFields(cl)]:
            for country_name in cb:
                print "Country:", country_name
                #setup where clause
                where = '"%s" = \'%s\'' % (CNTRY_NAME, country_name)
                arcpy.Select_analysis(os.path.join(dirpath), r"M:/Elevation_Masks/test_space/Country_Masks_Test/clipLayers_Test/clipLayers_Test.gdb/" + country_name + "_Boundary", where)
1

You need to either set cb to the first element of the list of lines cb = [line.strip() for line in f][0] or loop over the list of country names in your file.

with open('M:/Elevation_Masks/elevs.txt', 'r') as f:
    cb = [line.strip() for line in f]

CNTRY_NAME_FIELD = 'CNTRY_NAME'

for dirpath, dirnames, filenames in arcpy.da.Walk(cl, datatype="FeatureClass", type="ALL"):
    for filename in filenames:
        if CNTRY_NAME_FIELD in [f.name for f in arcpy.ListFields(cl)]:
            for country_name in cb:
                print "Country:", country_name
                #setup where clause
                where = '"%s" = \'%s\'' % (CNTRY_NAME_FIELD, country_name)

                arcpy.Select_analysis(os.path.join(dirpath, filename),
                                      r"M:\Elevation_Masks\Masks\clipLayers\" + country_name + "_Boundary",
                                      where)
  • Thanks for the response! Any reason the shapefiles would come back empty? I tried placing them in a GDB and as shapefiles but they are empty files? – cbrannin Jul 2 '14 at 13:47
  • All I can think is that the selection is somehow null. You're not getting any error messages? – Jason Scheirer Jul 2 '14 at 13:50
  • No, but when I print the country_name I get n as the result. So it has something to do with that I would guess. But one would think an error would be thrown if the selection was wrong. Right? – cbrannin Jul 2 '14 at 14:13
  • I think you applied both of my fixes then, you only need one. Get rid of the [0]. It's a valid where clause so it won't give an error, it just won't return any data. – Jason Scheirer Jul 2 '14 at 14:22
  • Wow! Thanks again for all the help. there were just a couple of small things that were tripping it up. Working code replaced in OP. Thanks again! – cbrannin Jul 2 '14 at 14:43
2

Your method assigns CB as a list not the individual lines in the file. Try opening your file like this:

f = open('M:/Elevation_Masks/elevs.txt','r')
for line in f:
    cb = line.strip('\n')
    # Carry on with the loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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