I am trying to make a script which selects every .png file in a folder beginning with the letters "LG". I then want the scipt create a shapefile, replacing the "LG" with "SH", and then i want the script to buffer that shapefile and rename the buffer with the first 2 letters being "SB".

I keep getting an error 99999 error message at line 37! ( gp.RasterToPolygon_conversion(INPUT_RASTER, Output_polygon_features, "SIMPLIFY", "VALUE") )

I have posted this on stackoverflow.com but probably here is a better place as scripting for arcmap is probably quite specialised!

Can anyone offer me any advice to why this script doesnt work? I am running ArcMap 9.3.1. and (attempting) to run the script from pyscripter!

# ---------------------------------------------------------------------------
# Created on: Wed Dec 01 2010 12:20:48 PM
#   (generated by ArcGIS/ModelBuilder)
# Usage: RASTER2POLYGON <INPUT_RASTER> <Output_polygon_features> <Raster_Buffer_shp>
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Conversion Tools.tbx")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")

# Script arguments...

folder = "D:\\J04-0083\\IMAGEFILES"
for root, dirs, filenames in os.walk(folder): # returms root, dirs, and files
    for filename in filenames:
        filename_split = os.path.splitext(filename) # filename and extensionname (extension in [1])
        filename_zero = filename_split[0]
        first_2_letters = filename_zero[0] + filename_zero[1]
        first_2_letters = "XX"

    if first_2_letters == "LG":
        Output_polygon_features = "D:\\J04-0083\\ShapeFiles.gdb\\" + "SH_" + filename + ".shp"

        # Process: Raster to Polygon...

        INPUT_RASTER = os.path.join(root + "\\" + filename_zero + ".png")
        gp.RasterToPolygon_conversion(INPUT_RASTER, Output_polygon_features, "SIMPLIFY", "VALUE") 

        Distance__value_or_field_ = "5 Meters"
        Raster_Buffer_shp = "SB_" + filename + ".shp"

        # Process: Buffer...
        gp.Buffer_analysis(Output_polygon_features, Raster_Buffer_shp, Distance__value_or_field_, "FULL", "ROUND", "NONE", "")
  • Does the conversion work when you try to do it manually?
    – AndOne
    Commented Dec 8, 2010 at 14:21
  • Sadly yes! So the problem lies in the script! I made it initially in model builder and it ran fine, its just now i want to run it for multiple files is there a problem!
    – Alice
    Commented Dec 8, 2010 at 14:28

4 Answers 4


Try changing

Output_polygon_features = "D:\\J04-0083\\ShapeFiles.gdb\\" + "SH_" + filename + ".shp"


Output_polygon_features = "D:\\J04-0083\\ShapeFiles\\" + "SH_" + filename + ".shp"

And rename your output directory accordingly.

I think ArcGIS is getting confused between creating a feature class called "SH_" + filename + ".shp" in a file geodatabase called ShapeFiles, and creating a shapefile in a folder called ShapeFiles.gdb

Caveat: this made the difference for me, but I have v10, and cannot test on 9.3


I answered you over on the ESRI Python Forum


Full annotation and reworked script, but the essence of the problem was trying to name the feature class in the File Geodatabase with a .shp extension. And then not cleaning up after each script run.

  • Thank you!!!! I have a new error now! I have posted the details on the ESRI forum!
    – Alice
    Commented Dec 9, 2010 at 7:41
  • My fault, problem was with selection logic to isolate the image file to be used. Corrected over on the ESRI Python forum thread as linked above. Commented Dec 9, 2010 at 15:07

I would suggest trying to print out the output at each step of your program. For example:

for root, dirs, filenames in os.walk(folder): # returms root, dirs, and files
    for filename in filenames:
        print filename
        filename_split = os.path.splitext(filename) 
        print filename_split
        filename_zero = filename_split[0]
        print filename_zero

An alternative could be to use, gp.AddMessage( filename ) etc, if you are running it from ArcGIS too.

Also, do you need '\' after "D:\J04-0083\IMAGEFILES" ?


Okay, here are a couple of things:

  1. I don't think the indention level of the code you posted is correct. The variable filename is used outside its scope, for example.

  2. Some uses of os.path.join in that code are pointless. The string fragments should be arguments: os.path.join(root, filename_zero + ".png")

  3. It is a good practice to avoid using try..except in this manner. Here is an alternative syntax that should do the same thing but is more Pythonic.

    for filename in filenames:
       name = path.splitext(filename)[0]
       prefix = name[0:2] or "XX"

Finally, use the pdb module to step through the code (link). Much easier to catch future input errors this way.

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.