Ascii to Raster Conversion help

2088
11
10-24-2011 10:16 AM
ClaireParsons
New 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
HemingZhu
Occasional Contributor III
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)


The issu is outputraster = raster.rstrip("dtm.asc"). It does not create "dtm". Try use
outputraster = raster.split(".")[0] instead
0 Kudos
ClaireParsons
New Contributor
dtm is in the file name so have used rstrip("dtm.asc") to strip dtm and asc from the filename so it doesn't exceed 13 characters - so not sure this is the answer as its the input file it can't find!

***Extra note - just ran your suggestion and same issue - for some reason it is saying the input file doesn't exist which is bizarre when it has got the name from the rasterlist which has just cycled through that folder!!! 
ERROR 000865: Input ASCII raster file: sx0051dtm.asc does not exist
0 Kudos
ScottBlankenbeckler
Occasional Contributor
env.workspace = "C:/Data/asc"


shouldn't this read env.workspace = r"C:/Data/asc" ??
0 Kudos
HemingZhu
Occasional Contributor III
dtm is in the file name so have used rstrip("dtm.asc") to strip dtm and asc from the filename so it doesn't exceed 13 characters - so not sure this is the answer as its the input file it can't find!

***Extra note - just ran your suggestion and same issue - for some reason it is saying the input file doesn't exist which is bizarre when it has got the name from the rasterlist which has just cycled through that folder!!! 
ERROR 000865: Input ASCII raster file: sx0051dtm.asc does not exist


Hard code the input and output path to see if it works.
0 Kudos
HemingZhu
Occasional Contributor III
shouldn't this read env.workspace = r"C:/Data/asc" ??


env.workspace = r"C:\Data\asc"
0 Kudos
ClaireParsons
New Contributor
Ok if you hard code the paths and run the script on a single file it works.
So, I'm wondering whether its ListRasters or the For loop - can't remeber having this problem before!!

EXTRA NOTE***** - Is there any other way I can get a list or loop through files in a folder?
0 Kudos
HemingZhu
Occasional Contributor III
Ok if you hard code the paths and run the script on a single file it works.
So, I'm wondering whether its ListRasters or the For loop - can't remeber having this problem before!!

EXTRA NOTE***** - Is there any other way I can get a list or loop through files in a folder?


env.workspace = "C:/Data/asc"
for file in arcpy.ListFiles("*.asc"):
0 Kudos
ClaireParsons
New Contributor
Hi thanks for your reply but this still isn't working.  I've changed the script a little bit so it prints the file names before commencing the conversion(or not!!), to ascertain that its creating the list, which incidentally it is.

import arcpy
from arcpy import env
import os

env.workspace = "C:\\Data\\asc"
listFiles = arcpy.ListFiles("*.asc")
for file in listFiles:
    outfile = file[:6]
    infile = file
    dtype = "Float"
    print file
    print outfile
    arcpy.ASCIIToRaster_conversion(infile, outfile, dtype)


I'm desperate for some advice on this as I have some files to convert for a client tomorrow 😞

thanks
Claire
0 Kudos
RaphaelR
Occasional Contributor II
maybe you can try the glob module instead of listrasters?

import glob,os
import arcpy

path = r'C:\Data\asc'
arcpy.env.workspace = path 
os.chdir(path)

try:

    # Make a list of asc files in the workspace
    listAsc = glob.glob('*.asc')
        
    for currentFile in listAsc:

        # Find the current file name and path
        name = currentFile[:-4]
        textFilePath = path + '/' + currentFile       

        # Create a raster from the current file
        arcpy.ASCIIToRaster_conversion(textFilePath,name + '.tif','FLOAT')
        print(arcpy.GetMessages() + "\n")    
    
        
# Get error messages
except:
    arcpy.GetMessages(2)
0 Kudos