I have a list of shapefiles that I need to select within a loop, to see if the file is there or not.
I tried several loops if, elif ... Just get an error???
I would appreciate any suggestions, to get this working.
#check file exists here?
fcList = arcpy.ListFeatureClasses()
for shapefile in fcList:
if shapefile == "Bus Stops":
print "1" ++ shapefile
elif shapefile == "Cluster_Sites":
print "2" ++ "shapefile"
elif shapefile == "Highway Boundary":
print "3" ++ "shapefile"
elif shapefile == "Street Lighting":
print "4" ++ "shapefile"
elif shapefile == "Road_Traffic_Collisions":
print "5" ++ "shapefile"
else:
print "end"
The code prints:
end
end
end
<type file>
There are 3 shapefiles in the directory.
There could be 0 to10 shapefiles
Solved! Go to Solution.
If you print the list returned by ListFeatureClasses, you may notice that shapefiles include ".shp". Try comparing against, for example, "Cluster_Sites_2014.shp" rather than "Cluster_Sites_2014".
Have you set the workspace environment? That is a necessary step 1.
Refer to help here.
could you format your code using code blocks since there are indentation errors which may be real or the result of copying/pasting/font issues
Clive,
Right off the bat, you have some syntax issues with ++ in your string concatenation (as well as some indentation issues - maybe just artifacts?). Also, you are referring to shapefile as an object and "shapefile" as a string. Try fixing that, and if it still doesn't work, print shapefile to make sure you know what it's doing. Lastly, are you in the right workspace? I don't see any code setting this...
datadir = arcpy.env.workspace = r"D:/DATA/HML"
try:
# If directory doesn't exist make it
if not os.path.isdir(datadir):
os.mkdir(datadir)
# Change the local directory to where you want to put the data
os.chdir(datadir)
#check file exists here?
fcList = arcpy.ListFeatureClasses()
for i in fcList:
if i == "Bus Stops":
print "1" ++ shapefile
elif i == "Cluster_Sites_2014":
print shapefile
else:
print "2"
except:
print"error"
There code runs, the appearance is copy/paste.
If the indentation was wrong, it would not compile!!
It prints:
2
2
2
<type file>
I need the loop to work.
If the file exists it runs a command, then jumps to the next item.
If the file does not exist it jumps to the next item.
In your code block, where is shapefile coming from? You still have an issue with your concatenation:
print "1" ++ shapefile
That should result in something like bad operand type for unary +: 'str'
Also, try just printing your item:
for i in fcList: print i
Hi Chris,
If I type:
fcList = arcpy.ListFeatureClasses()
for i in fcList:
print i
Then I get the list of shapefiles as expected.
Cluster_sites.shp
Fixed and Mobile Camera.shp
Road_Traffic_Collisions.shp
<type 'file'>
The aim is to use the if, elif block to use the CopyFeature command, if the shapefile exsists.
Thanks,
Clive
fcList = arcpy.ListFeatureClasses()
for i in fcList:
print "1" ++ shapefile
Returns an error
If you print the list returned by ListFeatureClasses, you may notice that shapefiles include ".shp". Try comparing against, for example, "Cluster_Sites_2014.shp" rather than "Cluster_Sites_2014".