AnsweredAssumed Answered

Name output files in "for" loop using input filenames

Question asked by aaridd18 on Mar 23, 2012
Latest reply on Mar 26, 2012 by bruce.nielsen
Hi all,

Apologies if this has been answered elsewhere; I'm a relative newcomer to Python (know just enough to get myself in trouble, usually). I'm working on a large script to geoprocess hundreds of files which will require outputs for each tool to be named as the for loop goes over them. Here's an example of part of the code:

for fc in arcpy.ListFeatureClasses ("*", "Point"):      outmean= ****     arcpy.MeanCenter_stats ('fc', 'outmean')


Where the ***** definition must iteratively rename the output file based on the input filename. The input files contain unique identifiers in the filename, like so:

samp01_0020_1986to1992_change_60m
samp01_0074_1986to1992_change_60m
samp05_0031_1992to2000_change_60m

The output file needs to contain the first 22 characters (i.e. samp01_0020_1986to1992) in order to be identified in the final output. Therefore, CreateUniqueName and CreateScratchName seem inappropriate because this part of the path couldn't be preserved (unless I'm mistaken). I thought some operation using os.path.join would be most appropriate, but the syntax is unclear to me--do I need to create a list of filenames, or can I use something like this:

for raster in arcpy.ListRasters:      outpoint = os.path.join (raster, "out")     arcpy.ExtractbyAttributes (raster, " VALUE = ", outpoint)


In addition, if I wanted to strip the end of the file (say, take off "change_60m") and add another ending, where should I start with the syntax? os.path.split and os.path.splitext seem inappropriate here, as I'm not interested in the entire path or the extension (neither are relevant, as the workspace is a single geodatabase).

Thanks in advance.

Outcomes