0

I want to use create random point tool on list of shapefiles, but keep failing to list the shape files. My code stops at line (print fcList); and gives me "None" as output.

Is it because of the version of ArcGIS?

I am using ArcGIS 10.6 Version.

The code successfully works, and creates random points for a single shapefile.

import arcpy
import os
from arcpy import env
from arcpy.sa import *
env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")
arcpy.env.workspace= "C:‪/Users/ru/Desktop/Project"
outpath =  "C:/Users/ru/Desktop/Project/points"
fcList = arcpy.ListFeatureClasses()
print fcList
for fc in fcList:
    cursor = arcpy.da.SearchCursor(fc, ["num_pt"])
    with arcpy.da.SearchCursor(fc, ["num_pt"]) as cursor:
        for row in cursor:
            num = row[0]
            arcpy.CreateRandomPoints_management(outpath, "sa.shp"+ str(num), fc, "", num)
    del row
    del cursor
    print "points created"

In Python window, ArcMapIn pyscripter (different results with same path/wokspace)

  • Your out name will be sa.shp1, sa.shp2 etc.. try 'sa{}.shp'.format(str(num).zfill(4)) to write in the format sa0001.shp, sa0002.shp etc. Are the shapefiles in the folder set as your workspace? Your code looks fine so it should only return None if there are no featureclasses in that folder. – Michael Stimson Dec 4 '18 at 0:43
  • Again, the del cursor is unnecessary when with is used. Having two search cursors named "cursor" is wrong; pick one. – Vince Dec 4 '18 at 0:52
  • @MichaelStimson. Thank you for suggestion. Yes there are several shapefiles in the workspace but the problem remains the same. – nima Dec 4 '18 at 0:57
  • Can you open ArcCatalog, browse to the folder, open the python window and execute arcpy.env.workspace = r'c:\users\ru\desktop\project' then print arcpy.ListFeatureClasses() then grab a screen shot of catalog and add to your question please (edit out any personal info with Paint or similar). @Vince has a point, it's better to use with arcpy.da.SearchCursor(fc,["num_pt"]) as cursor: (indent under with statement), this will automatically release resources when the block ends. – Michael Stimson Dec 4 '18 at 1:02
  • how do I add images to this block? its not allowing me. do I create new question and insert the image you asked? FYI: in python window, Arcmap, it prints all the shapefiles, but in Pyscripter, it still print "None" for the exact same workspace. – nima Dec 4 '18 at 1:46

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.