Pulling numbers from ASCII filename not working

IamIan iansan at gmail.com
Wed Jan 25 15:42:20 EST 2006

Thank you for the replies, I'm new to Python and appreciate your
patience. I'm using Python 2.1.

To reiterate, the ASCII files in the workspace are being read correctly
and their latitude values (coming from the filenames) are successfully
being converted to string. Even doing LatInt = int(LatString) works,
however the second I try to print LatInt's value or use it in
mathematical operations, the code chokes in ArcGIS.

My full code:

# Import system modules
import sys, os, win32com.client

# Create the geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
print gp.usage("Hillshade_sa")
print gp.usage("RasterToOtherFormat_conversion")
print gp.usage("DefineProjection_management")

# Check license availability
gp.AddMessage ("ArcInfo license is " + str(gp.CheckProduct("ArcInfo")))

# Set workspace
workspace = "E:\\GISTest"
gp.workspace = workspace
gp.AddMessage("Workspace = " + gp.workspace)

filenames = os.listdir(gp.workspace)
filenames = [filename.lower()
for filename in filenames
if (filename[-4:].lower() == ".asc" and filename[0] != "-" )]
for filename in filenames:

   # For each ASCII file, create Hillshade.
   # account for latitude by computing Z units using radians
   Latitude = filename[1:3]
   LatString = str(Latitude)
   LatInt = int(LatString)
   gp.AddMessage("LatInt is " + LatInt)
   radians = LatInt * 0.0174532925
   zFactor = 1/(113200 * (cos(radians)))

The complete traceback:

Traceback (most recent call last):
  File "e:\python21\pythonwin\pywin\framework\scriptutils.py", line
310, in RunScript
    exec codeObject in __main__.__dict__
  File "E:\Documents and
Settings\Administrator\Desktop\Ian\GIS\Python\zOnly.py", line 32, in ?
    gp.AddMessage("LatInt is " + LatInt)
TypeError: cannot add type "int" to string

I tried print repr(filename) and it returned the actual filename:
'n16w099.asc' , 'n17w062.asc' , etc.

More information about the Python-list mailing list