4.12.2005

Glob Code

The code I got to work is as follows. I then used elements in the list to run my ASCII to GRID algorithm. This code only runs under the PythonWin GUI. I have yet to get it functioning within ArcGIS 9x. You may need to do some minor modification since you will be cutting/pasting the code into PythonWin. You will also need to change path names.
NC

#Author: Naticus Cartographica
#Created: April 12, 2005
#Batch ESRI ASCII format to ESRI GRID format Conversion
#Python 2.1 for use with ArcGIS 9.x

#Import standard/necessary library modules
import win32com.client, sys, os, glob, string

#Create the Geoprocessor object
GP = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

#Set the input/output workspace
GP.workspace = "C:/Naticus/python_test"
#GP.workspace = sys.argv[1]
print "The name of the workspace is " + GP.workspace
GP.addmessage ("The name of the workspace is: " + GP.workspace )
#Check for this toolbox (not absolutly needed)
#GP.Toolbox = "Conversion"

#Set the output workspace
#outWorkspace = sys.argv[2]
outpWorkspace = "C:/Naticus/python_test"

#create the file list with the .txt file extension
#this is a temporary file, but could be made to an output file
#Python has a structure called a "glob" that allows the parsing
#of file/string names

filelist = glob.glob('*.txt')

#loop through the list of files and process each one
#indenting is important, since Python uses this format for processing
for filename in filelist:
if GP.exists(filename):
print "Input file: " + GP.workspace + "/" + filename + " does exist"
GP.addmessage ("Input file: " + GP.workspace + "/" + filename + " does exist" )

#create output file by removing the .txt extension and adding grd to
#the end of the file (this is not an extension, just a trailer)
#the format of the output file is a ESRI GRID format
outgrid = filename[:-4] + "grd"
print "Output file: " + outWorkspace + "/" + outgrid



elif GP.exists(outgrid):
print outgrid + " already exists"

# Process: ASCIIToRaster_conversion

GP.ASCIIToRaster_conversion(filename, outgrid, "FLOAT")

print "Finished converting ASCII file: " + filename + " to: " + outgrid + " GRID file."


except:
GP.AddMessage(GP.GetMessages(2))
print GP.GetMessages(2)




No comments: