1

I am wondering if anyone has solved this or can help me on my code. I have a Parcel shapefile, and I would like to create folders based on the attribute data (per row) from a field in the shapefile; the field is FID. The folders will eventually be used for relative hyperlinks.

My error comes in the line os.makedirs(os.path.join(PF,row.FID)); I know the rest of the code works when I comment this out - Python returns all the values per row> I want to create new folders from these values.

import os, arcpy

featureclass = "W:\\Shapefile\\Shapefile.shp"
PF = "W:\\12345678\\Project\\Parcel"

rows = arcpy.SearchCursor(featureclass)
row = rows.next()

while row:
    print row.FID
    os.makedirs(os.path.join(PF,row.FID))
    row = rows.next()
4
  • 1
    It would help if you would include what version of ArcGIS Desktop you are using (which would help tell which version of Python you are using and which features within arcpy are available).
    – John
    Mar 3, 2015 at 21:53
  • Isn't it a bit dangerous to use FID as an identifier? If you happen to edit your shapefile or make any other changes to it, the FID's might change as well. I'd make another field that's created from the FID values, but not base any data connections on the FID field.
    – Martin
    Mar 4, 2015 at 7:25
  • It is used as an example that everyone reading can understand without seeing the attribute fields of the shapefile. Thanks Martin.
    – Mike
    Mar 4, 2015 at 19:19
  • Hi John, I am using 10.2 for Arc and Python is 2.7.5. The below comments solved the problem.
    – Mike
    Mar 4, 2015 at 19:20

2 Answers 2

4

Without seeing your error message it is hard to be certain, and I prefer to use arcpy.CreateFolder_management() in place of os.makedirs, but try changing:

os.makedirs(os.path.join(PF,row.FID))

to

os.makedirs(os.path.join(PF,str(row.FID)))
3
  • In this case you're right, CreateFolder would work as PF is known to exist, I prefer os.makedirs() because it makes any folder that doesn't already exist, for example if you said os.makedirs("c:\\temp\\my\\working\\folder") and only c:\temp exists then it would make folders my, working and folder... this can also be dangerous if you have a spelling mistake earlier in the tree. Mar 3, 2015 at 23:18
  • why the hell esri is offering a replacement for makedirs?
    – nickves
    Mar 4, 2015 at 21:01
  • 1
    @nickves, my guess is for implementation in model builder.
    – Fezter
    Mar 5, 2015 at 0:35
3

First, I Suggest you to use SearchCursor from data access modulde for better performance.

rows = arcpy.da.SearchCursor(featureclass)

The problem with you code is that you are joining string with int (FID). just convert it to string.

os.makedirs(os.path.join(PF,str(row.FID)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.