1

I'm using the arcpy.MakeGraph_management and arcpy.SaveGraph_management functions to create & save graphs in a Python Toolbox. The code works initially; however, subsequent use of the tool results in an error saying that the output from arcpy.MakeGraph_management (i.e., "temp_graph") already exists. If I close ArcMap and reopen it, I can again use this code fine, suggesting that the output from arcpy.MakeGraph_management is temporary; however I can't figure out how to delete this temporary graph in order to run the tool in succession or create multiple graphs in the same script.

# User provided input
folder = parameters[0].valueAsText
name = parameters[1].value
arcpy.env.workspace = folder

# Reference files
referenceFiles = os.path.join(os.path.dirname(__file__), "Reference Files")
graph_template = os.path.join(referenceFiles, "template_graph.grf")

# Create graph
arcpy.MakeGraph_management(graph_template, [...], "temp_graph")
arcpy.SaveGraph_management("temp_graph", "{0}/{1}_graph.pdf".format(folder, name))   

Attempts at solution so far:

1)

arcpy.MakeGraph_management(graph_template, [...], "temp_graph")
arcpy.SaveGraph_management("temp_graph", "{0}/{1}_graph.pdf".format(folder, name))  
arcpy.Delete_management("in_memory")

2)

if arcpy.Exists("temp_graph"):
    arcpy.Delete_management("temp_graph")
    arcpy.MakeGraph_management(graph_template, [...], "temp_graph")
    arcpy.SaveGraph_management("temp_graph", "{0}/{1}_graph.pdf".format(folder, name)) 
else:
    arcpy.MakeGraph_management(graph_template, [...], "temp_graph")
    arcpy.SaveGraph_management("temp_graph", "{0}/{1}_graph.pdf".format(folder, name)) 

Yet these tries still get me the same error: Yet these tries still get me the same error

I found someone with the same issue posted years ago: https://geonet.esri.com/thread/55685

I posted a response there in addition to posting it to Stack Exchange.

Edit (5/11/17): I've discovered that the tool will run successfully if I set arcpy.env.overwriteOutput = True, although I'd prefer an actual solution with deleting "temp_graph". I had tried @Midavalo's suggestion to use arcpy.CreateUniqueName, which was unsuccessful, but forgot to include it in the original post.

I've noticed that despite my attempt to setup a Current Worskpace, it's not working.: enter image description here

This is so despite the code where I attempt to set my current workspace and also try using arcpy.Describe() to print the path of where "temp_graph" is located.

param0 = arcpy.Parameter(
    displayName="Output Folder",
    name="in_workspace",
    datatype="DEWorkspace",
    parameterType="Required",
    direction="Input")
...
folder = parameters[0].valueAsText
arcpy.env.workspace = folder
arcpy.AddMessage("current workspace is {0}".format(arcpy.env.workspace))
desc = arcpy.Describe("temp_graph") OR desc = arcpy.Describe("{0}/temp_graph".format(folder))
print(desc.dataElement.catalogPath)

enter image description here

2

You could try using CreateUniqueName. This tool appends incrementing numbers to the end of an input name until it finds one that is unique, e.g. temp_graph_3

uniquename = arcpy.CreateUniqueName("temp_graph")
arcpy.MakeGraph_management(graph_template, [...], uniquename)
arcpy.SaveGraph_management(uniquename, "{0}/{1}_graph.pdf".format(folder, name))  
  • arcpyCreateUniqueName() isn't working in this context, not sure why... – Edward May 11 '17 at 20:24

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.