1

I have different month (JANUARY, FEBRUARY, MARCH.....DECEMBER) folder, each folder hold point shapefile for different month. I want to consider all month folder in a single program and output pass into as on different month folder.

I have written a code but its taking only single month at a time. How to put in my code different month workspace and output save into as same month folder?

I have shown below my code. This code taking too long time for merging the monthly point shapefile. Is there any function available for process the program?

import arcpy, os


out = r'D:\SWAT-WEATHER-DATA2\MARCH' 
arcpy.env.workspace = r'D:\SWAT-WEATHER-DATA2\MARCH'  
shplist =  arcpy.ListFeatureClasses('*.shp')
arcpy.Merge_management(shplist, os.path.join(out, 'Merged_003.shp'))
print 'done' 
1

You can step through all your folders and files using arcpy.da.walk() and merge the listed files for each of the folders.

This will get all shapefiles in each folder (if that folder is the name of a month) and merge them together into a new file stored in the same folder. It will get the number of the month (i.e. January = 001, March = 003 etc.) and the output file will be based on this number e.g. all shapefiles in the folder OCTOBER will be merged into a new shapefile called Merged_010.shp

import arcpy, os, calendar
topWorkspace = r'D:\SWAT-WEATHER-DATA2' 

# Get dict of months and month-number (i.e. January = 001, March = 003 etc.)
months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)} 

# Get list of all folders that contain feature classes (shapefiles)
walk = arcpy.da.Walk(topWorkspace, topdown=True, datatype="FeatureClass") 

for folderPath, folderNames, fileNames in walk: 
    baseName = os.path.basename(folderPath).upper() 
    if baseName.upper() in months: # Test that subfolder is a month name
        monthNumber = months[baseName] # Get month-number for use in output filename
        arcpy.env.workspace = folderPath 
        shapeFiles = [x for x in fileNames if os.path.splitext(x)[1].lower() == '.shp'] 
        # Merge all shapefiles in folder into new shapefile with name like Merged_001.shp (for January)
        arcpy.Merge_management(shapeFiles, "Merged_{}.shp".format(monthNumber)) 

I have modified the script a little to get around the arcpy.da.Walk() not working. This instead uses arcpy.ListWorkspaces() to find all the folders, and then arcpy.ListFeatureClasses() to find all the shapefiles in each folder.

import arcpy, os, calendar
topWorkspace = r'D:\SWAT-WEATHER-DATA2' 
arcpy.env.workspace = topWorkspace

# Get dict of months and month-number (i.e. January = 001, March = 003 etc.)
months = {calendar.month_name[i].upper(): str(i).zfill(3) for i in range(1, 13)} # Get dict of months and month number (i.e. January = 001, March = 003 etc.)

# Step through list of all folders
for folderPath in arcpy.ListWorkspaces():
    baseName = os.path.basename(folderPath).upper()
    if baseName in months: # Test that subfolder is a month name
        monthNumber = months[baseName] # Get month-number for use in output filename
        arcpy.env.workspace = folderPath
        shapeFiles = arcpy.ListFeatureClasses('*.shp')
        # Merge all shapefiles in folder into new shapefile with name like Merged_001.shp (for January)
        arcpy.Merge_management(shapeFiles, "Merged_{}.shp".format(monthNumber)) # Merge shapefiles in folder into new shapefile with name Merged_001.shp (for January)
  • Thank you. I ran the code , following error i am getting time of execution Message File Name Line Position Traceback <module> <module1> 8 AttributeError: 'module' object has no attribute 'Walk' – Shouvik Jha Aug 21 '16 at 3:18
  • I am using 10.1 – Shouvik Jha Aug 21 '16 at 3:28
  • @ShouvikJha - that is odd, as arcpy.da.Walk() was introduced at 10.1. Have you run it as above or did you modify anything? It could be rewritten without the Walk if you can't get that to work. – Midavalo Aug 21 '16 at 3:35
  • No , I didn't modify anything. I just ran the same code. – Shouvik Jha Aug 21 '16 at 3:37
  • @ShouvikJha I have updated my answer to include a version of the script without the arcpy.da.Walk(). – Midavalo Aug 21 '16 at 4:16

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.