1

I'm looking to add a large number of point files into an .mxd from a huge group of multi-layer folders. The script from Loading multiple shapefiles from +1000 subfolders worked with some tweaks, but doesn't return anything presumably since all of the files have different names after the prefix (i.e. Subject Baltimore / Subject Aberdeen / Subject Rockville etc).

Is there a way to add something to tell the script to look at just the first part of the shapefile's label? here is the code I'm using:

import os

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

workspace = "T:\\GIS\\Special Projects\\ESRI UC MAP 2017\\Maryland"    #place the path to your data inside the quote marks, remembering to use forward slashes
feature_classes = []

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Point")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

for dataset in feature_classes:
    dataset = dataset.upper()
    if "Subject" in dataset:
        add_layer = arcpy.mapping.Layer(dataset)
        arcpy.mapping.AddLayer(df, add_layer)
  • Have you tried to use all caps for the string "Subject"? That is, "SUBJECT" – reevesii May 24 '17 at 14:12
1

I believe the string in your if statement needs to be capitalized. The upper() method returns a copy of the string where all case-based characters are uppercased. In order to match the characters in dataset with those in your string, they may need to be capitalized. See string in if statement below.

import os

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

workspace = "T:\\GIS\\Special Projects\\ESRI UC MAP 2017\\Maryland"    #place the path to your data inside the quote marks, remembering to use forward slashes
feature_classes = []

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="Point")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))

for dataset in feature_classes:
    dataset = dataset.upper()
    if "SUBJECT" in dataset:
        add_layer = arcpy.mapping.Layer(dataset)
        arcpy.mapping.AddLayer(df, add_layer)
  • YES! That totally worked! Thank you! None of the shapefile labels are capped so I wasn't sure about that one....if we got rid of that line would it still work? I'm a super beginner with Python so forgive my ignorance. – LucyM May 24 '17 at 16:06
  • Great! A good way to show thanks is to mark an answer as accepted. Keep at it. – reevesii May 24 '17 at 16:11
  • My understanding is that the upper() method is used to deal with issues of discrepancies in capitalization. You should try to see what happens when that line is absent. – reevesii May 24 '17 at 16:19
  • Awesome - thanks for the tips - First time posting here :) - I'll try it with out, thanks! – LucyM May 24 '17 at 18:17
1

Basic but important question: Are you running this python script within ArcMap? Using arcpy.mapping.MapDocument(“CURRENT”) requires to be run within the MXD. It can't be run "outside" of MXD environment.

I agree with reevesii's comment. Try making all caps on “”subject” so it looks like this:

if “SUBJECT” in dataset:

The line Before says the dataset is put into all caps by dataset.upper() . Please note that all layers you add to MXD this way will in caps.

  • YES! That totally worked! I am running it through ArcMap only. Thank you! – LucyM May 24 '17 at 16:04

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.