Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a simple tool which outputs a file. I would like the filename to be a number which increments every time the tool is run.

How would I go about implementing this using the toolbox in ArcMap 10?

share|improve this question
up vote 9 down vote accepted

Assuming your tool is always outputting the output file in the same directory, this function will return the next highest-numbered file name:

import os

def getNextFilePath(output_folder):
    highest_num = 0
    for f in os.listdir(output_folder):
        if os.path.isfile(os.path.join(output_folder, f)):
            file_name = os.path.splitext(f)[0]
                file_num = int(file_name)
                if file_num > highest_num:
                    highest_num = file_num
            except ValueError:
                'The file name "%s" is not an integer. Skipping' % file_name

    output_file = os.path.join(output_folder, str(highest_num + 1))
    return output_file

To get the next output file's path, simply print the function:

print getNextFilePath(<YOUR_OUTPUT_PATH>)

You could use this in your tool's ToolValidator class to generate this as default "output file" value, or you could include it in the tool's code itself, if you want the tool to simply accept an output folder for the generated file.

share|improve this answer
Nice one. I think it might need to be tweaked depending on how the files should be named and if the file extension needs to be included. I would change this function to just return the next highest number, and then have other functions to format the filename as required. – blah238 Jun 13 '12 at 18:38
Excellent, this will do the trick! – Beau Bouchard Jun 13 '12 at 19:16
I shared this with a few other grad students, Thank you so much again, great answer – Beau Bouchard Jun 14 '12 at 17:19
You're welcome! I'm glad I could help. If I get a moment I might fine tune it. – Tanner Jun 14 '12 at 17:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.