Select to view content in your preferred language

I want to create a folder using an attribute value from a feature

1736
6
Jump to solution
07-30-2013 04:34 AM
OLANIYANOLAKUNLE
Frequent Contributor
This is my code and the error message received also

mxd = arcpy.mapping.MapDocument("Current") df = arcpy.mapping.ListDataFrames(mxd)[0] fc = "Parcels" field = "StateName" cursor = arcpy.SearchCursor(fc)  for row in cursor:  val = row.getValue(field) arcpy.CreateFolder_management("C:\\"+ "\\" + Str(val) + "\\")  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 000735: Folder Name: Value is required Failed to execute (CreateFolder).


Any suggestions?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
Ah that is because that tool requires two parameters, the path and the folder name separate.

arcpy.CreateFolder_management("C:\\", str(val))


http://resources.arcgis.com/en/help/main/10.1/index.html#//0017000000pz000000

It would be easier to just use os.makedirs()

View solution in original post

0 Kudos
6 Replies
MathewCoyle
Honored Contributor
Str shouldn't be capitalized. Also string substitution would be the preferred method for these kinds of operations, which eliminates the need for the str operator at all.

arcpy.CreateFolder_management("C:\\{0}".format(val)


The way I create folders is using os.makedirs(). This creates all directories recursively if they do not exist which helps if you want to create folders where the parent folders may or may not exist.
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
I still have the same error check it below:

>>> mxd = arcpy.mapping.MapDocument("Current")
>>> df = arcpy.mapping.ListDataFrames(mxd)[0]
>>> fc = "Parcels"
>>> field = "StateName"
>>> cursor = arcpy.SearchCursor(fc)
>>> for row in cursor:
...     val = row.getValue(field)
...     
>>> arcpy.CreateFolder_management("C:\\{0}".format(val))
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 000735: Folder Name: Value is required Failed to execute (CreateFolder). 
0 Kudos
MathewCoyle
Honored Contributor
Ah that is because that tool requires two parameters, the path and the folder name separate.

arcpy.CreateFolder_management("C:\\", str(val))


http://resources.arcgis.com/en/help/main/10.1/index.html#//0017000000pz000000

It would be easier to just use os.makedirs()
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Thanks that works perfectly
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Please can you help me with this:

I want to create the following Nested folders i.e.

- StateName - arcpy.CreateFolder_management("C:\\", str(val))
- LGAName(DistrictName) this would be created inside the StateName Folder - arcpy.CreateFolder_management("C:\\", str(val),outName)
- PlotNo(BlockNo) this would be created inside the LGAName(DistrictName) Folder - arcpy.CreateFolder_management("C:\\", str(val)+ outName,outName1)
- arcpy.mapping.ExportToPDF(mxd,r"C:\\", str(val) + outName + "\\" + outName1 + "\\" + "Land Granted To " + outName2)

I'm having this "cant take more than two arguments issue especially with the last two create folder statements and the export to pdf statement
this was the script I used below

fc = "Parcels"
field = "OBJECTID"
field1 = "Plot_No"
field2 = "Block_No"
field3 = "Name_Allottee"
field4 = "TDP_Status"
field5 = "LGA"
field6 = "District"
field7 = "Shape.STLength()"
field8 = "SitePlan_Status"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
#row.getValue(field)
   val = row.getValue(field)
   val1 = row.getValue(field1)
   val2 = row.getValue(field2)
   val3 = row.getValue(field3)
   val4 = row.getValue(field4)
   val5 = row.getValue(field5)
   val6 = row.getValue(field6)
   val7 = row.getValue(field7)
   val8 = row.getValue(field8)

outName = str(val5) + "_LGA" + "(" + str(val6) + "_Area" + ")"
outName1 = "Plot_" + str(val1) + "(" "Block_" + str(val2) + ")"
outName2 = str(val3) + " Block_" + str(val2) + "_Plot_" + str(val1) + "_" + str(val6) + "_Area_of_" + str(val5) + "_LGA" + ".pdf"
arcpy.CreateFolder_management("C:\\", str(val),outName)
arcpy.CreateFolder_management("C:\\", str(val)+ outName,outName1)
arcpy.mapping.ExportToPDF(mxd,r"C:\\", str(val) + outName + "\\" + outName1 + "\\" + "Land Granted To " + outName2)
os.startfile(r"C:\\", str(val) + outName + "\\" + outName1 + "\\" + "Land Granted To " + outName2)


Kindly assist please. Thanks
0 Kudos
MathewCoyle
Honored Contributor
0 Kudos