Name output files in "for" loop using input filenames

485
4
Jump to solution
03-23-2012 11:44 AM
AnneRiddle
New Contributor
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.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BruceNielsen
Occasional Contributor III
You want to use slicing to extract a subset of a string. To get the first 22 characters of a string use:
outmean = fc[:22]
which returns the characters in positional offset 0 thru 21. Then append the rest of your output file name with the append symbol, '+'. To complete it all in one step, it will look something like:
outmean = fc[:22] + "newending"

View solution in original post

0 Kudos
4 Replies
BruceNielsen
Occasional Contributor III
You want to use slicing to extract a subset of a string. To get the first 22 characters of a string use:
outmean = fc[:22]
which returns the characters in positional offset 0 thru 21. Then append the rest of your output file name with the append symbol, '+'. To complete it all in one step, it will look something like:
outmean = fc[:22] + "newending"
0 Kudos
StacyRendall1
Occasional Contributor III
Anne,

Bruce's answer should work. I just noticed that you need to be careful with your variables, i.e. where these are already strings. For example:
for fc in arcpy.ListFeatureClasses ("*", "Point"): 
    outmean= ****
    arcpy.MeanCenter_stats ('fc', 'outmean')

You have 'fc', but fc is already a string - it is a variable, whose name is fc, that contains the feature class name, i.e. '2009_Street_Centrelines'... If you wrap the fc in quotes, then you are passing a new string, literally just fc, not the contents of the variable fc...

Not sure if you require this, but if you need things to be unique, you can just make a custom counter to ensure uniqueness of the names, i.e.:
index = 1
for fc in arcpy.ListFeatureClasses ("*", "Point"): 
    outmean= fc[:22] + '_' + index
    arcpy.MeanCenter_stats (fc, outmean) # ok, I don't know what exactly is going on here, but hopefully that works...
    index += 1 # increment the index


Let me know how you get on!
0 Kudos
AnneRiddle
New Contributor
Bruce, thank you--that is exactly what I was looking for. Stacy, indexing incrementally shouldn't be necessary here, as the slicing operation will preserve the Unique ID in the filename, but I'll stash that idea for the future. I'll let you know how it goes!
0 Kudos
BruceNielsen
Occasional Contributor III
You're Welcome. If you click the checkmark next to the correct response, it will let others doing searches later on know that your question was answered.
0 Kudos