1

I'm running a process that includes generating a set of 3 curvature layers. For various reasons, sometimes I may generate just 1, 2, or all 3. Later I may need to go back and generate another layer, but the complication is that to create the first set of layers I used a randomly generated temporary surface. So if I were to create a sibling curvature layer, it wouldn't match the original. What I do, then, is to see if any of the 3 layers are missing. If so, I delete and recreate them all 3. But there must be a cleaner way.

curvLayers = [curvpath, profpath, planpath]
if (not arcpy.Exists(curvpath) or
  not arcpy.Exists(profpath) or
  not arcpy.Exists(planpath)):
    for layer in curvLayers:
        if arcpy.Exists(layer): DM.Delete(layer)
        <create new layers>

I'd love to think there was something like:

if not all arcpy.Exists(curvLayers):
5
  • 2
    You can try any function of Python with a list comprehension with Exists.
    – fatih_dur
    Oct 5, 2016 at 6:30
  • So, if any(arcpy.Exists(c) for c in curvLayers):? But I need to test for 'all', not 'any'.
    – joechoj
    Oct 5, 2016 at 8:22
  • Perhaps set arcpy.env.overwriteOutput=True. Then just run the tools and allow them to overwrite existing files if they find. Although it will not work for certain tools. Oct 5, 2016 at 8:56
  • 1
    Your original conditional, (not arcpy.Exists(curvpath) or not arcpy.Exists(profpath) or not arcpy.Exists(planpath)), can be translated into any context (because all conditional connectors are or) as if any(not arcpy.Exists(c) for c in curvLayers).
    – fatih_dur
    Oct 5, 2016 at 21:36
  • Interesting: putting not within the parentheses provides yet another permutation. Thanks for clarifying; I wasn't picking up on that from your first comment. I'll reference this in my solution.
    – joechoj
    Oct 5, 2016 at 22:23

1 Answer 1

2

The all() function does the trick. The code above can be condensed as follows:

curvLayers = [curvpath, profpath, planpath]
if not all(arcpy.Exists(c) for c in curvLayers): #"If any files missing..."
    for layer in curvLayers:
        if arcpy.Exists(layer): DM.Delete(layer)
        <create new layers>

The first if clause above evaluates to True if one or more are missing, and False if all files exist.


Note I'm using not to manipulate the logic of the all() function, which flips the result. Alternate examples, for clarity:

if all(arcpy.Exists(c) for c in curvLayers): #"If all files exist..."

or...

if any(arcpy.Exists(c) for c in curvLayers): #"If any files exist..."

or...

if not any(arcpy.Exists(c) for c in curvLayers): #"If all files missing..."

Alternatively, I suppose I could use the else clause instead of the not to flip the result, but using not makes the code more readable. Thanks @fatih_dur for getting me so close to the solution.

EDIT: As @fatih_dur points out up top, yet another variation in the logic (putting not inside the function) can accomplish the same thing:

if any(not arcpy.Exists(c) for c in curvLayers): #"If any files missing..."

I'll refrain from building this variation into all the other options, as it would undermine my efforts at clarity!

Your Answer

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

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