0

I have a Python script that takes several input parameters, one of which is the name of an output folder which will hold processed data. I want to be able to access that output folder variable as an output parameter as well. How do I do this? Note: This script is being used in an ArcGIS model. I need that output parameter to be an input for another process in the model.

#Get the input feature class, optional fields and the output filename
input_FC = sys.argv[1]
inField = sys.argv[2]
theFName = sys.argv[3]
outFolder = sys.argv[4]
3
  • 5
    What's an "output parameter"? Please be specific about what you want to do with it. Commented Mar 8, 2018 at 22:08
  • Sorry, what I really mean is just having the outFolder, which is initially an input variable, also be an output once the process is complete. As I mentioned, I am using this script in a GIS model builder where variable can be referred to as input of output "parameters." Commented Mar 9, 2018 at 15:24
  • This is a question about the ArcGIS ModelBuilder and incorporating Python script tools written using ArcPy into it. Consequently, it is far better asked at the Geographic Information Systems Stack Exchange. Commented Mar 11, 2018 at 11:58

2 Answers 2

1

Python is about to "output" variables (or parameters as you called them) in a few ways. If you would like the variables to be written to screen, you can use the print function.

input_FC = sys.argv[1]
inField = sys.argv[2]
theFName = sys.argv[3]
outFolder = sys.argv[4]

print outFolder

If you instead want this variable to be used for a different python script, you might want to consider writing a function. Functions can take variables as input and use them to do calculations, then return outputs.

def complete_fname(folder, fname):
    output = folder + '/' + fname
    return output

input_FC = sys.argv[1]
inField = sys.argv[2]
theFName = sys.argv[3]
outFolder = sys.argv[4]

outFName = complete_fname(outFolder, theFName)

Lastly, you may want to consider placing your sys.argv commands inside of a conditions which only runs the commands if your script is run from the command line. This way the program will not be looking for command line arguments if someone used "import" on your script instead of running it from the command line.

def complete_fname(folder, fname):
    output = folder + '/' + fname
    return output

if __name__ == '__main__':

    input_FC = sys.argv[1]
    inField = sys.argv[2]
    theFName = sys.argv[3]
    outFolder = sys.argv[4]

    outFName = complete_fname(outFolder, theFName)
Sign up to request clarification or add additional context in comments.

Comments

1

If you have to put it in as an input, don't you have that information already? How are you calling the function if you don't know what your inputs are? Store the input as a variable before calling it, then use that variable wherever else you need it.

1 Comment

No, it's literally just the name of an empty folder in which the processed results are going to go. So that input variable is just an empty folder name with no data until the process/function runs. The problem stems from trying to use that same variable (the output folder name) as an input variable in a model. When I use it as an input in the model, it needs to be filled with the processed data.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.