AnsweredAssumed Answered

Add layer from current folder

Question asked by hafizi_rotu on Nov 20, 2017
Latest reply on Dec 11, 2017 by hafizi_rotu

Hye everyone,


I have create one Python file, the task is to add layer from the latest or current folder following date of folder. The sample folder is below;


Sample of folder


This is my sample code on Python;

import arcpy
import os

# suppose you want to add it to the current MXD (open MXD)
mxd = arcpy.mapping.MapDocument(r"C:\Users\User\Documents\MTSB_JPS\MXD\test.mxd")
dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]

# base folder
workspace = r"C:\Users\User\Documents\MTSB_JPS\Flood_Forecasting_Outputs\TRN"
walk = arcpy.da.Walk(workspace, topdown=False, datatype="FeatureClass")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        layerfile = os.path.join(dirpath, filename)
        addlayer = arcpy.mapping.Layer(layerfile)
        arcpy.mapping.AddLayer(dataFrame, addlayer, "BOTTOM")

del addlayer, mxd


The problem is, this code can't add the layer from the latest or current folder. It should add the layer from "20171117_0600_AF" but when I run this code, it add the layer from "20170701_2000_AF" folder.


Please help.