Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

This is a continuation of the question I asked here:

At this point I'm really just trying to get everything in one place and keep the original shapefile basename. I'll tackle merging like-named files later, which is why I'm using CopyFeatures for now. I'm confused about why the second sample below didn't have a result list similar to the first one. It looks like the first sample ran through the first folder and copied those shapefiles to the gdb, and when it got to the next folder with a 'Bank_Ero' name it failed.

So now, I'd like to create a unique file name (keeping that original shp base name) if one already exists. As always, any hints, or direction will be greatly appreciated, and thanks for looking.

Original sample:

for dirname, dirnames, filenames in os.walk ("C:\FOR MIKEK\GPS Files\GPS_ANDY"):
    for subdirname in dirnames:
        env.workspace = os.path.join(dirname, subdirname)
        All_List = arcpy.ListFeatureClasses()
        for shp in All_List:
            outFeatureClass = os.path.join("C:\FOR MIKEK\Python_Scratch.gdb", shp.strip(".shp"))
            arcpy.CopyFeatures_management(shp, outFeatureClass)


<Result 'C:\\FOR MIKEK\\Python_Scratch.gdb\\Bank_Ero'>
<Result 'C:\\FOR MIKEK\\Python_Scratch.gdb\\Fish_Bar'>
<Result 'C:\\FOR MIKEK\\Python_Scratch.gdb\\Flag'>
<Result 'C:\\FOR MIKEK\\Python_Scratch.gdb\\Outfall'>

Traceback (most recent call last):
  File "<pyshell#82>", line 7, in <module>
    arcpy.CopyFeatures_management(shp, outFeatureClass)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 1943, in CopyFeatures
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000725: Output Feature Class: Dataset C:\FOR MIKEK\Python_Scratch.gdb\Bank_Ero already exists.
Failed to execute (CopyFeatures).

and now attempting to use CreateUniqueName:

import arcpy, os
>>> from arcpy import env
>>> env.workspace = "C:\FOR MIKEK\GPS Files\GPS_ANDY"
>>> for dirname, dirnames, filesnames in os.walk ("C:\FOR MIKEK\GPS Files\GPS_ANDY"):
    for subdirname in dirnames:
        env.workspace = os.path.join(dirname, subdirname)
        shpList = arcpy.ListFeatureClasses()
        for shp in shpList:
            outFC = os.path.join("C:\FOR MIKEK\Python_Scratch.gdb", shp.strip (".shp"))
            if arcpy.Exists(outFC):
                arcpy.CreateUniqueName(os.path.join, "C:\FOR MIKEK\Python_Scratch.gdb")
                arcpy.CopyFeatures_management(shp, outFC)


>>> xferFiles = arcpy.ListFeatureClasses("C:\FOR MIKEK\Python_Scratch.gdb")
>>> print xferFiles
[]
share|improve this question
    
What version of ArcGIS for Desktop are you using? If it is 10.1 SP1 then you may find arcpy.da.Walk helps. –  PolyGeo Apr 17 '13 at 22:07
add comment

1 Answer 1

It looks like this is working for me:

import arcpy
>>> import os
>>> from arcpy import env
>>> env.workspace = "C:\FOR MIKEK\GPS Files\GPS_ANDY"
>>> for dirname, dirnames, filenames in os.walk("C:\FOR MIKEK\GPS Files\GPS_ANDY"):
    for subdirname in dirnames:
        env.workspace = os.path.join(dirname, subdirname)
        shpList = arcpy.ListFeatureClasses()
        for shp in shpList:
            outFC = arcpy.CreateUniqueName(os.path.join("C:\FOR MIKEK\Python_Scratch.gdb", shp.strip(".shp")))
            arcpy.CopyFeatures_management(shp, outFC)

@PolyGeo thanks for the suggestion. I'm using ArcGIS Desktop 10 SP4 and Python 2.6.5

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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