2

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)
4

You have to get the mxd parameter in the Python code as a string first and then create an ArcMap document object. In the script tool properties, you can still use the parameters data type you've specified, but you cannot pass directly this object to the arcpy code.

    mxd = arcpy.GetParameterAsText(0)
    mxd_doc = arcpy.mapping.MapDocument(mxd) #create an mxd object

The whole code:

import arcpy, os

try:
    #Set input parameters
    mxd = arcpy.GetParameterAsText(0)
    outputTxtFile = arcpy.GetParameterAsText(1) #r"D:\10_GIS\Projects\Maps\Test2.txt"

    #Open txt file
    outputTxtFileWrite = open(outputTxtFile, "w")

    lyrList = []

    mxd_doc = arcpy.mapping.MapDocument(mxd) #create an mxd object
    for lyr in arcpy.mapping.ListLayers(mxd_doc):
        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)
  • Thanks Alex, I had originally coded it that way but tried to be nifty and try and pass the object directly..obviously doesn't work that way. I have changed my code and is now working mostly due to your changes, and not using the mxd I initially had been testing the code on. It turned out to be corrupted, and most likely the cause of my initial problems. Thanks again for your quick response! – RedChilli Jan 28 '15 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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