Pulling numbers from ASCII filename not working

Larry Bates larry.bates at websafe.com
Tue Jan 24 15:59:10 EST 2006


At lest one of the filenames (or directories) being returned by
os.listdir doesn't have integer value where you are looking.
Remember that os.listdir returns subdirectories as well as files.
You may want to look at using glob.glob() instead and limit it
to *.asc.

Secondly,

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

is better rewritten as (not tested):

import glob
filenames=glob.glob(os.path.join(gp.workspace, '*.asc'))
filenames = [f.lower() for f in filenames if not f.startswith('-')]

Larry Bates

IamIan wrote:
> I searched the archives but couldn't find anyone else with this
> problem. Basically I'm grabbing all ASCII files in a directory and
> doing geoprocessing on them. I need to calculate a z-factor based on
> the latitude of the ASCII file being worked on, which is in the
> filename. If I type in the code manually it works and reads the
> latitude value from the ASCII filename, but when run within ArcGIS it
> crashes when it gets to int(LatString). Isnumber() returned false for
> Latitude as well. Is there something different about reading values
> from an ASCII filename?
> 
> import sys, os, win32com.client, string, gc
> 
> # Get a list of ASCII files in the workspace for ASCII To Raster
> conversion
> 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)
>         radians = LatInt * 0.0174532925
>         zFactor = 1/(113200 * (cos(radians)))
> 



More information about the Python-list mailing list