I am trying to get a Map Document and (a new) text file as user defined parameters, in order to write to the text file a list of layer sources. The text file is then suppose to open once complete.
When I hard code in a test mxd and txt inputs the code works, however when I use arcpy.GetParameter/arcpy.GetParameterAsText and set up the code within a Toolbox script, the code doesn't work. It will not create the text file, or even populate a test text file (if hard coded).
I have also tried: outputTxtFile = arcpy.GetParameter(1) with no success.
I am wondering if I am using arcpy.GetParameter/arcpy.GetParameterAsText correctly?
Note: in the Toolbox script properties, I have the first parameter set to 'ArcGIS Map Document' and the second set to 'Text file'.
# Import system modules
import arcpy, os
try:
#Set input parameters
mxd = arcpy.GetParameter(0)
outputTxtFile = arcpy.GetParameterAsText(1) #r"D:\10_GIS\Projects\Maps\Test2.txt"
#Open txt file
outputTxtFileWrite = open(outputTxtFile, "w")
lyrList = []
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.supports("DATASOURCE"):
lyrSource = lyr.dataSource
lyrList.append(lyrSource)
lyrList.sort()
for i in lyrList:
outputTxtFileWrite.write(i + "\n")
arcpy.AddMessage("Writing to file")
outputTxtFileWrite.close()
os.startfile(outputTxtFile)
except Exception as e:
print arcpy.GetMessages()
arcpy.AddMessage(e)