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?

1 Answer 1


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.

  • 3
    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
    Commented Jun 13, 2012 at 18:38
  • 1
    Excellent, this will do the trick! Commented Jun 13, 2012 at 19:16
  • 1
    I shared this with a few other grad students, Thank you so much again, great answer Commented Jun 14, 2012 at 17:19
  • You're welcome! I'm glad I could help. If I get a moment I might fine tune it.
    – Tanner
    Commented Jun 14, 2012 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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