-4

This question already has an answer here:

How would you skip the arcpy.Merge and arcpy.Clip if 'AAA010.shp' does not exist in the directory that os.walk is going through. I will have more code after the last line in this snippet.

import os, fnmatch, arcpy

arcpy.env.workspace  =  r’C:\Users\sysadmin\desktop\datamanagement\MGCP.gdb’

fcMerged = ‘MergedFeatureClass’

fcClipped = ‘ClippedFeatureClass’

fcToClip = r’C:\Users\sysadmin\desktop\datamanagement\Country.shp’

filesMatched = list()

for root, subfolders, files in os.walk(r’C:\Users\sysadmin\desktop\datamanagement\MGCP_Raw’):

  for f in fnmatch.filer(files, ‘AAA010.shp’):
    filesMatched.append(os.path.join(root,f))

if 'AAA010.shp' in filesMatched:

    arcpy.Merge_management (filesMatched, fcMerged)

    arcpy.Clip_analysis (fcMerged, fcToClip, Extraction_Mine)

else: pass

marked as duplicate by PolyGeo Mar 4 '16 at 0:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    This is both a duplicate and off-topic as not pertaining to GIS. – Richard Law Mar 4 '16 at 0:45
  • I'm curious as to how its unrelated to GIS? Can you shed some light on that @RichardLaw so I know how I should be asking my questions about python. – Brian J Mar 4 '16 at 0:48
  • 1
    I agree with @RichardLaw that this is a duplicate of your previous question and pure Python (i.e. suitable for researching at Stack Overflow) because where you are stuck is before using (or at least needing to use) any ArcPy functions. You have set arcpy.env.workspace but not accessed that prior to where you are stuck. For ArcPy help here we really need you to start breaking down your code (and code you find elsewhere) into shorter code snippets that illustrate where you are stuck. We do not offer a code debugging/writing/improving service but we will help with code snippets. – PolyGeo Mar 4 '16 at 1:08
  • I think the way our focussed Q&A format works generally will make a lot more sense if you review meta.gis.stackexchange.com/questions/3349/… and with respect to coding questions if you review meta.gis.stackexchange.com/questions/3994/… and meta.gis.stackexchange.com/questions/4006/… – PolyGeo Mar 4 '16 at 1:18
2

The below code will only perform the merge management and clip analysis if the file name 'AAA010.shp' is in your list after the walk.

if 'AAA010.shp' in filesMatched:
    arcpy.Merge_management(filesMatched, fcMerged)
    arcpy.Clip_analysis(fcMerged, fcToClip, Extraction_Mine)
else: pass

This of course assumes all your variables used in the merge and clip have been set up correctly.

  • 2
    Note, this will also work without the else statement. – Fezter Mar 4 '16 at 0:29
  • 1
    @BrianJ After you run the walk, print out the resulting list. What's in the list? – nwduncan Mar 4 '16 at 0:45
  • 1
    Can you paste in exactly what is printed out so we can see? – nwduncan Mar 4 '16 at 0:51
  • 1
    OK. The above code will only work if the string 'AAA010.shp' is in the list. Remember, strings are case sensitive when truth testing in python. For example, 'aaa010.shp' or 'AAA010.SHP' are not the same as 'AAA010.shp'. Make sure the string you're testing for is in the list exactly as in the if statement. – nwduncan Mar 4 '16 at 1:09
  • 1
    It looks like in your code the resulting list will be filled with strings containing the full path name of your shape files. Testing for 'AAA010.shp' will not return True. I would consult the answers to your previous question as they seem to answer it quite well. – nwduncan Mar 4 '16 at 1:21

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