Data Driven Pages - Creating folders

329
4
10-30-2018 06:05 AM
LieslDe_Swardt1
New Contributor II

Hi All,

I am new to python, and was wondering if there is a way to incorporate folder creation as part of my data driven page script, see below:

mxd = arcpy.mapping.MapDocument("CURRENT")

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

...

... in_folder = arcpy.GetParameterAsText(0)

... file_name = arcpy.GetParameterAsText(1)

...

... for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):

...     mxd.dataDrivenPages.currentPageID = pageNum

...     pageName = mxd.dataDrivenPages.pageRow.Name

...     arcpy.mapping.ExportToJPEG(mxd, r"C:\Folder path\ " + pageName + "_Landcapability.jpeg", resolution=250) 

... del mxd

... del df

So basically I have 12 data sets consisting of either raster or vector data sets and one main data set (properties) which forms my data driven page. What I will do is, I will for instance switch on my land capability data set and run the script, when it is finished I will switch the next data set on and run the script again until I finished all 12 data sets. My pageName is the property code, therefore I will have 12 maps per property code. I then go and create folders for each property code, lets say 001 and move all 12 maps into that folder, and so forth for the rest. Is there a way of optimising this process, so that I don't manually have to switch on and of the layers and also so that I don't have to go manually create folders for each property code. It is over a 1000 properties, so currently it is a very time consuming process.

Any help will be appreciated.

0 Kudos
4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

The accepted answer in this link is safe.. there are old (aka python 2.7) and current methods covered in the thread

exception - How can I safely create a nested directory in Python? - Stack Overflow 

LieslDe_Swardt1
New Contributor II

Thanx Dan this helps a lot, I am still a bit confused though, not sure where to incorporate the script in my script. I should actually state that I have no knowledge in python at all   .

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

grief... why are you starting with the python api then

You have to import a module before you can use it.  So let's skip 'safe' and just get to the point.  

In this case, your script needs to include

import os

os.makedirs("c:/Temp/Test")  # enter the path to create

Use forward slashes for now (read up on 'raw' encoding for later)

If it doesn't work, failure will eventually bring enlightenment

Welcome to python... and start learning python 3.x (ie ArcGIS PRO's python) since 2.7 is terminal (yeah yeah, skip the it will be around for a long time stories)

0 Kudos
LieslDe_Swardt1
New Contributor II

Lol, I am just gonna pretend that I know exactly what Python api is   .

So that means I will do the import os first, then put my data driven pages script, and then lastly put the os.makedirs script?

This is actually an urgent process I was tasked with, so I think I will get it done in arcmap and then move over to ArcGIS Pro. 

But thanks hey - you are of great help

0 Kudos