gwydion93
gwydion93

Reputation: 1923

Turning an input parameter into an output parameter in Python

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]

Upvotes: 0

Views: 2871

Answers (2)

michaelmedford
michaelmedford

Reputation: 176

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)

Upvotes: 1

krflol
krflol

Reputation: 1155

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.

Upvotes: 1

Related Questions