1

The looping process I'm working on outputs two files (table and image), and I'm trying to name them with matching numbers. I'm using arcpy.CreateUniqueName to generate new filenames into a GDB each time the loop runs. The problem is, while the first CreateUniqueName usage produces new filenames in the expected sequence each time I run it, the second usage just keeps overwriting the original file, with filenames never progressing past the starting filename.

Here's the code, followed by the resulting print statements from 3 sequential runs:

out_prof = r'F:\Joe_School\Thesis\scripts\Jerry\scratch.gdb\testprof'
patch = r'F:\Joe_School\Thesis\data\grid_extract\mask400_1'

import arcpy, os
from arcpy import env
import arcpy.ddd as DDD
import arcpy.management as DM

env.overwriteOutput = True
env.outputCoordinateSystem = arcpy.Describe(patch).spatialReference
env.workspace = os.path.dirname(out_prof)

arcpy.CheckOutExtension('3D')
tablename = arcpy.CreateUniqueName('prof_table')
graphname = arcpy.CreateUniqueName('prof_graph')
prof = DDD.StackProfile(out_prof, patch, tablename, graphname)
DM.SaveGraph(graphname, graphname + '.gif')
arcpy.CheckInExtension('3D')
print "Created files:" + "\n    " + tablename + "\n    " + graphname

>>> 
Created files:
    F:\Joe_School\Thesis\scripts\Jerry\scratch.gdb\prof_table
    F:\Joe_School\Thesis\scripts\Jerry\scratch.gdb\prof_graph
>>> 
Created files:
    F:\Joe_School\Thesis\scripts\Jerry\scratch.gdb\prof_table0
    F:\Joe_School\Thesis\scripts\Jerry\scratch.gdb\prof_graph
>>> 
Created files:
    F:\Joe_School\Thesis\scripts\Jerry\scratch.gdb\prof_table1
    F:\Joe_School\Thesis\scripts\Jerry\scratch.gdb\prof_graph

Why are these seemingly identical statements behaving differently?

1
  • 2
    It looks like StackProfile must enable the out_table to increment its names when it encounters an existing one of the same name. To test that, and show that this is an issue that purely involves StackProfile rather than ArcPy just run the tool three times from its tool dialog.
    – PolyGeo
    Sep 13, 2016 at 20:49

2 Answers 2

3

I think there are a couple of issues here and arcpy does what it is supposed to do.

The first one is you are setting env.workspace to os.path.dirname(out_prof) which a GDB. Unlike Windows File structure, GDB has a special meaning for ArcGIS, therefore arcpy. That's why when you look into GDB via ArcCatalog you just see these items correctly and without any file extension (I will come to this later). However the Windows Explorer shows you the content of this GDB as higgledy-piggledy. Basically once you ask arcpy to create a unique name, it looks for feature classes, rasters, tables, etc inside this GDB (your env.workspace variable) and return a name suggestion. So if it is a table, it produces correct name and lets you to save your output. But here, in fact arcpy gives you a table name (i.e., prof_graph variant(s)) and you are adding GIF extension to this name, which is undefined file extension type within GDB context (you can find this GIF in this GDB folder, which was overwritten with each loop) and arcpy cannot recognise this as a proper data item (feature class, table and so on). More clearly it looks for a GDB data item like F:\Joe_School\Thesis\scripts\Jerry\scratch.gdb\prof_graph but it cannot find it because it has a gif extension. Then it suggests the same name for each loop.

The second issue is related to the very last point of the previous item, saving legitimate files into a GDB (though this is simply a folder to Windows) which is a terrible practice (believe me). Please don't do that.

Therefore I suggest you to save you GIFs into a folder not in a GDB, that's where arcpy will give you right file name suggestion. Namely change this part in your code and you will be fine: graphname = arcpy.CreateUniqueName(os.path.join(r'F:\Joe_School\Thesis\scripts\Jerry','prof_graph.gif')).

1
  • Much appreciated! The part about a GIF being unrecognizable within a GDB was very helpful.
    – joechoj
    Sep 20, 2016 at 8:31
3

The arcpy.CreateUniqueName() produces an entire file path, not just a file name. If not specified, this is based on the current environment workspace. This means that the code:

arcpy.env.workspace = r"C:\Temp"
uName = arcpy.CreateuniqueName('MyUniqueName')
print uName

will produce

C:\Temp\MyUniqueName

If you want it in a different folder, then you can add that different folder as a second argument

arcpy.env.workspace = r"C:\Temp"
uName = arcpy.CreateuniqueName('MyUniqueName', r"C:\SomewhereElse\MyDB.gdb")
print uName

will produce

C:\SomewhereElse\MyDB.gdb\MyUniqueName

In your line DM.SaveGraph(graphname, graphname + '.gif') it is trying to save the new raster with a .gif extension inside your geodatabase which it cannot do. You either need to specify a different path outside your GDB, or save the raster without the extension into your GDB.

If saving to a file path (and not to a GDB) you can specify a file extension as part of your arcpy.CreateuniqueName()

arcpy.env.workspace = r"C:\Temp"
uName = arcpy.CreateuniqueName('MyUniqueName.txt')
print uName

will output

C:\Temp\MyUniqueName0.txt

if there is already a MyUniqueName.txt in that folder.

0

Your Answer

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

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