Home > Blockchain >  How to change output filename in a for loop? ArcPy
How to change output filename in a for loop? ArcPy

Time:09-30

I am using an ArcPy function (ExtractByAttributes) in a for loop. I have a series of files names ("i") in a list (test). They are rasters for which I want to run a process in ArcPy.

The for loop basically builds the path to find each raster in the first line and then runs the ArcPy process (Extract by Attributes). As written now, the loop runs correctly but in each iteration it overwrites the result file. How can I rewrite this so the variable "result" has a different name in every iteration?

for i in test:
    path = folder_path   "\\"   i   "\\"   i   '.tif'
    result = ExtractByAttributes(path,"VALUE = 10 OR VALUE = 30")

Thanks a lot in advance :)

CodePudding user response:

You could make an array of the same length and store the results in there.

results = [len(test)]
for i in range(len(test)):
    path = folder_path   "\\"   test[i]   "\\"   i   '.tif'
    results[i] = ExtractByAttributes(path,"VALUE = 10 OR VALUE = 30")

CodePudding user response:

Solved:

for i in rasters:
    path = folder_path   "\\"   i   "\\"   i   '.tif'
    print(path)
    result = ExtractByAttributes(path,"VALUE = 10 OR VALUE = 30")
    result.save(folder_path   "\\"   str(i)   ".tif")
  • Related