Select to view content in your preferred language

Loop issue

3752
10
Jump to solution
12-22-2015 09:19 AM
CliveSwan
Frequent Contributor

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

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

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".

View solution in original post

10 Replies
DarrenWiens2
MVP Honored Contributor

Have you set the workspace environment? That is a necessary step 1.

Refer to help here.

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

ChrisSmith7
Honored Contributor

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...

CliveSwan
Frequent Contributor

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.

ChrisSmith7
Honored Contributor

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'

0 Kudos
ChrisSmith7
Honored Contributor

Also, try just printing your item:

for i in fcList:
   print i
CliveSwan
Frequent Contributor

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

0 Kudos
CliveSwan
Frequent Contributor

    fcList = arcpy.ListFeatureClasses()

    for i in fcList:

        print "1" ++ shapefile

Returns an error

0 Kudos
DarrenWiens2
MVP Honored Contributor

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".