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)
env.workspace = "C:/Data/asc"
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
shouldn't this read env.workspace = r"C:/Data/asc" ??
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?
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)
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)