1

How do I provide a default value like recommended in every Arcpy script? This script is exported from ModelBuilder where every output except the last one is set to temporary and managed. I want this path to show to the default geodatabase every user has set on his system regardless of the exact path. I am new to programming so I want to apologize if this is a very basic task.

    Input__DEM_ohne_Hecke = arcpy.GetParameterAsText(0)
    if Input__DEM_ohne_Hecke == '#' or not Input__DEM_ohne_Hecke:
    Input__DEM_ohne_Hecke = "H:\\Zwischenlager Patriot\\Projektarbeit_6\\Test_Verschattung.gdb\\Hoehen_Untersuchungsgebiet" # provide a default value if unspecified

2 Answers 2

1

To answer the 'default' part of your question, you could:

  • make the input parameter optional.
  • if no input was given, do something like the following to set a value

    Input__DEM_ohne_Hecke = arcpy.GetParameterAsText(0) or "H:\Zwischenlager Patriot\Projektarbeit_6\Test_Verschattung.gdb\Hoehen_Untersuchungsgebiet" # if no input provided to the param, 'or' is used to set a value

However, I'd suggest not doing that. It'll confuse whoever is using your script when they leave a parameter blank and then the output is magically written to some fgdb on their disk (which may or may not exist).

The best way to set a default value into the script tool itself would be to use Hornbydd suggestion. But again, setting a hard coded default value here assumes you have some knowledge and assurance of a directory/fgdb that already exists.

I'd suggest re-thinking this though. If you need to write output to a fgdb and you absolutely need to be sure it exists, without writing a bunch of code to do checks and build your own, you simply could write to either

  • arcpy.env.scratchGDB (use this inside the script when building paths)
  • %scratchGDB% (you could use this with the suggestion above as a default value)

You can do some reading about this variable here. You'd use this inside the script, and then not even expose it as a parameter. You the script author just handle the output path. The user of your tool doesn't ever have to worry about setting/defining it. (This is personally what I'd do based on my understanding of the question)

0

Looking at the sample code you have uploaded I see you are using the GetParameterAsText() method which tells me you are wiring up the python script to an ArcToolbox script tool interface. In this case when you are defining the interface and the parameters that you will pass into the script you can set the default there. It is discussed on this page.

1
  • Also, in the code above, the last line would need to be indented.
    – recurvata
    Commented Oct 1, 2014 at 12:20

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.