Select to view content in your preferred language

Ascii to Raster Conversion help

2901
11
10-24-2011 10:16 AM
ClaireParsons
Emerging Contributor
Hope someone can help - I've written this script several times in the past but unfortunately my laptop is being serviced and I forgot to backup my python scripts directory - ~DOH 😮

Anyway, this should be really simple - I'm looping through some ascii files in a directory and converting them to a raster.  However, Python returns the error - Failed to execute (ASCIIToRaster).
I know the ascii file is there as I have manually converted in toolbox and its fine.  I've posted the code below.

# Import arcpy module
import arcpy
from arcpy import env

# Local variables:
env.workspace = "C:/Data/asc"
outputraster = raster.rstrip("dtm.asc")
datatype = "FLOAT"

#Get a list of ascii files
rasterList = arcpy.ListRasters()


for raster in rasterList:
    arcpy.ASCIIToRaster_conversion(raster,outputraster,datatype)
Tags (2)
0 Kudos
11 Replies
JustinDix
New Contributor
I too am trying to batch convert 137 asc files to raster using the python script - I can make the function work for individual files:

import arcpy
arcpy.ASCIIToRaster_conversion("c:/data/elevation.asc", "c:/output/elevation", "INTEGER")

But cannot get it to batch work. I tried to follow the thread below (although I couldnt see if it was actually successful?) but to no avail. This is my first day of Python so could just be my ignorance. Any help greatfully received.
0 Kudos
WhitneyKirkendall
Deactivated User
Just add the path to the name of the ascii file for each member of your list as you go through a loop.

I got it to work like this...

for asc in ascList:
[INDENT]asc = path+"\\"+asc[/INDENT]
[INDENT]arcpy.ASCIIToRaster_conversion(asc, asc[:-4]+".img" , "FLOAT")[/INDENT]
0 Kudos