Load data (shp and features) out of multiple folders and subfolders in one FGDB

1073
8
Jump to solution
08-08-2016 06:20 AM
JohannesBierer
Regular Contributor
## Load Data

import arcpy
import os

workspace = r"R:\Daten\geo_daten_Kopie"
out_workspace = r"R:\Karto\Bierer2016\OrdnerRDatenGeodatenBereinigen\SIC_Geodaten.gdb\data"

import arcpy
import os

feature_classes = []
for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, datatype="FeatureClass"): ##, type="Polygon"):
    for filename in filenames:
        feature_classes.append(os.path.join(dirpath, filename))
        name = os.path.join(dirpath, filename)
        print name
        BaseName = os.path.basename(name)
        print BaseName
        BaseNameCopy = BaseName.split(".shp")
        print BaseNameCopy
        arcpy.CopyFeatures_management(filename, os.path.join(out_workspace, BaseNameCopy))

The code leads to this error message:

Traceback (most recent call last):

  File "H:/python_scripte/ListFolderandShapes/ListFeatures_FoldersSubfolders_CopyFeatures_3.py", line 22, in <module>

    arcpy.CopyFeatures_management(filename, os.path.join(out_workspace, BaseNameCopy))

  File "C:\Python27\ArcGIS10.2\lib\ntpath.py", line 73, in join

    elif isabs(b):

  File "C:\Python27\ArcGIS10.2\lib\ntpath.py", line 58, in isabs

    return s != '' and s[:1] in '/\\'

TypeError: 'in <string>' requires string as left operand, not list

Anyone has got an idea for a better solution?

0 Kudos
1 Solution

Accepted Solutions
FC_Basson
MVP Regular Contributor

Your output basename created after the split creates a list, so you need to access the first item of the list and not the entire list:

BaseNameCopy = BaseName.split(".shp")
print BaseNameCopy[0]
 arcpy.CopyFeatures_management(filename, os.path.join(out_workspace, BaseNameCopy[0]))

View solution in original post

0 Kudos
8 Replies
FC_Basson
MVP Regular Contributor

Your output basename created after the split creates a list, so you need to access the first item of the list and not the entire list:

BaseNameCopy = BaseName.split(".shp")
print BaseNameCopy[0]
 arcpy.CopyFeatures_management(filename, os.path.join(out_workspace, BaseNameCopy[0]))
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Please mark questions as questions, not discussions, so credit can be given for providing a correct answer.

0 Kudos
JohannesBierer
Regular Contributor

Ah ok, didn't get this detail. Will post it again as a question. I think if FC Basson answers again the same he will get the points ...

0 Kudos
FC_Basson
MVP Regular Contributor

Double points for double work!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Can you still edit the discussion to mark it as a question?  I thought you were allowed to for a certain period of time after creating the discussion, but I may be mistaken.

0 Kudos
JohannesBierer
Regular Contributor

Can't find the possibility to change a discussion to a question?

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

I converted it to a question.  and will add a couple tags.

Edit:

Johannes Bierer I also marked on of the answers as correct.  If that is not the one you wanted, you can unmark, and mark a different one.

Joshua Bixby  lol.  I'm not moderator on all, but this one I could do. 

JoshuaBixby
MVP Esteemed Contributor

Great, next time I will just ping you.

0 Kudos