I Need Your Help Please

628
7
08-25-2012 06:40 PM
OLANIYANOLAKUNLE
Occasional Contributor II
I am trying to generate a create a folder and the name of this folder would be derived from an attribute value by using the searchCursor and at the same time generate a pdf output which name would also be derived from an attribute valve using the search cursor. This my code below, ive been having serious issues with the folder path as the application claims my output destination does not exist.

arcpy.SelectLayerByLocation_management("Parcels", "COMPLETELY_WITHIN", "ClipFeature", "", "NEW_SELECTION")
fc = "Parcels"
field = "LGA"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    row.getValue(field)
val = row.getValue(field)
field = "District"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    row.getValue(field)
val1 = row.getValue(field)
field = "Block_No"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    row.getValue(field)
val2 = row.getValue(field)
field = "Plot_No"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    row.getValue(field)
val3 = row.getValue(field)
outName = str(val) + "LGA" 
outName = "Block" + str(val) + "Plot_" + str(val2)
outName1 = "Block_" + str(val2) + "Plot_" + str(val3) + str(val1) + "Area of" + str(val) + "_LGA" + ".pdf"
outName2 = str(val1) + "AREA" 
outName3 = "Block" + str(val2) #+ "Plot_" + str(val3)
outName4 = "Plot_" + str(val3)
arcpy.CreateFolder_management("C:\ETE_STATE",outName,outName2,outName3,outName4)
arcpy.mapping.ExportToPDF(mxd,"C:\ETE_STATE", outName+outName2+outName3+outName4+ outName1)


Please your suggestions would be appreciated. Thanks (Ive also included my script as an attachment)
Tags (2)
0 Kudos
7 Replies
MathewCoyle
Frequent Contributor
Use this to create directories.

os.makedirs(path)
0 Kudos
curtvprice
MVP Esteemed Contributor
Also - watch your paths. Single back slashes may be interpreted incorrectly on windows. They need to be double or the string must be prefixed by "r".

arcpy.CreateFolder_management("C:\\ETE_STATE",outName,outName2,outName3,outName4)


Arc 10 help: Paths explained
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
I tried to use your last suggestion and this was what i got;

>>> arcpy.CreateFolder_management("C:\\ETE_STATE",outName,outName1,outName2,outName3)
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: CreateFolder() takes at most 2 arguments (5 given)
>>>



I tried to nest outName1 in outName using the code below and i got another error message;

>>> arcpy.CreateFolder_management("C:\\KWARA_STATE\\outName",outName1)
Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 14477, in CreateFolder     raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Folder Location: Dataset C:\KWARA_STATE\outName does not exist or is not supported Failed to execute (CreateFolder).  
>>> 

What do you think?
0 Kudos
ChristopherThompson
Occasional Contributor III
arcpy.CreateFolder_management("C:\\KWARA_STATE\\outName",outName1)

Does the folder outName exist yet? This has to already be created in order to put another folder in it.  If you are supplying 'outName' as a parameter then that has to sit outside your quoted string so it would look something like this:
outName = 'FolderX'
outName1 = 'NewFolder'
arcpy.CreateFolder_management("C:\\KWARA_STATE\\" + outName,outName1)


Also in that first example, it looks like you are trying to create several folders at once:
arcpy.CreateFolder_management("C:\\ETE_STATE",outName,outName1,outName2,outName3)

The way to do this would be to create a list of those folders, then iterate that in a 'for loop' like this:
folders = [outName,outName1,outName2,outName3'
for folder in folders:
     arcpy.CreateFolder_management("C:\\ETE_STATE",folder)
0 Kudos
RobertStewart
New Contributor II
If it's any help, the way I got around this was to create a variable for the pathname and then another variable for the folder name.

PATHNAME = "C:\\KWARA_STATE"

arcpy.CreateFolder_management(PATHNAME, Outname1{or whatever your folder name is})


I have found that when using entered or derived variables, it was easier to combine them first, then pass that onto the script.

For what it's worth.

Cheers!
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Thank u clthompson for your suggestions, i've been able to create my folders by combining two variables at a time since the create folder function can only take 2 arguments at a time this is my code;

>>> outName6 = str(val) + "_LGA" + "(" + str(val1) + " _Area" + ")"
>>> outName1 = "Plot_" + str(val3) + "(" "Block_" + str(val3) + ")"
>>> arcpy.CreateFolder_management("C:\\ARA_STATE\\", outName6)
<Result 'C:\\ARA_STATE\\rin_South_LGA(GRA_Buo_Oho _Area)'>
>>> arcpy.CreateFolder_management("C:\\ARA_STATE\\"+ outName6,outName1)
<Result 'C:\\ARA_STATE\\rin_South_LGA(GRA_Buo_Oho _Area)\\Plot_9(Block_9)'>


My next problem is how to drop my pdf in the last folder, i used this code and got this errror message;

>>> outName2 = "TDP_FOR_" + "Block_" + str(val2) + "_Plot_" + str(val3) + "_" + str(val1) + "_Area_of_" + str(val) + "_LGA" + ".pdf"
>>> arcpy.mapping.ExportToPDF(mxd,"C:\\ARA_STATE\\" + outName6,outName1,outName2)
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\mapping.py", line 1133, in ExportToPDF
    assert isinstance(data_frame, DataFrame) or (isinstance(data_frame, basestring) and data_frame.lower() == 'page_layout')
AssertionError
>>> arcpy.mapping.ExportToPDF(mxd,"C:\\ARA_STATE\\" + outName6,outName1)
Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\mapping.py", line 1133, in ExportToPDF
    assert isinstance(data_frame, DataFrame) or (isinstance(data_frame, basestring) and data_frame.lower() == 'page_layout')
AssertionError


What do you think please. Thanks
0 Kudos
ChristopherThompson
Occasional Contributor III
Again, as with your initial example, it looks like you're trying to pass too many arguments into the function:
arcpy.mapping.ExportToPDF(mxd,"C:\\ARA_STATE\\" + outName6,outName1,outName2)

The help on this shows the following:
ExportToPDF (  map_document,   out_pdf,   {data_frame},   {df_export_width} , {df_export_height}, {resolution}, {image_quality}, {colorspace}, {compress_vectors}, {image_compression}, {picture_symbol}, {convert_markers}, {embed_fonts}, {layers_attributes}, {georef_info}, {jpeg_compression_quality})


In the above examples i've color coded the matching elements - you can see that the items outname1 and outname2 would want to match up to the optional arguments that the tool accepts for specifying the data frame and export width. Its not clear to me what you are trying to do here. The tool wants you to supply a map document name (which you have as 'mxd') and the name of a pdf file to out put, which from your code would be 'C:\\ARA_STATE\\" + outName6' - but from your earlier work I think that is a folder.. so I'm left confused with what outname1 and outname2 are supposed to be.
0 Kudos