Help: looping funtion to creat a list for the files from different folders

1040
19
08-11-2011 02:20 PM
ZiaAhmed
New Contributor III
I am very new in Python.  I am trying to import several featue classess into file geodatabse (name: IA.gdb)  using a script. These files are in located in diffrent folders. For example: 
K:\SUURGO_Data\IA\soil_ia001\soil_ia001\spatial\soilmu_a_ia001.shp
K:\SUURGO_Data\IA\soil_ia001\soil_ia003\spatial\soilmu_a_ia003.shp
K:\SUURGO_Data\IA\soil_ia001\soil_ia005\spatial\soilmu_a_ia005.shp
K:\SUURGO_Data\IA\soil_ia001\soil_ia053\spatial\soilmu_a_ia053.shp
K:\SUURGO_Data\IA\soil_ia001\soil_ia193\spatial\soilmu_a_ia193.shp
.... so on

I used model bulider to do this for a  few number of files. But I want to do this with a loop to select (list)  all of these files  and import to a filegeodata base. I do not know how to write a loop function get the list of files located in different folders and subfolders.   Help will be highly appreciated.  Below find a python script - saved from Model Bulder.

Thanks
Zia

--------------------------------------------------------------------------
# ImprtFetureGeodatabse.py
# Created on: 2011-08-11 17:44:46.00000
#   (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy


# Local variables:
soilmu_a_ia001_shp = "K:\\SUURGO_Data\\IA\\soil_ia001\\soil_ia001\\spatial\\soilmu_a_ia001.shp"
soilmu_a_ia003_shp = "K:\\SUURGO_Data\\IA\\soil_ia003\\soil_ia003\\spatial\\soilmu_a_ia003.shp"
soilmu_a_ia005_shp = "K:\\SUURGO_Data\\IA\\soil_ia005\\soil_ia005\\spatial\\soilmu_a_ia005.shp"
soilmu_a_ia197_shp = "K:\\SUURGO_Data\\IA\\soil_ia197\\soil_ia197\\spatial\\soilmu_a_ia197.shp"
soilmu_a_ia053_shp = "K:\\SUURGO_Data\\IA\\soil_ia053\\soil_ia053\\spatial\\soilmu_a_ia053.shp"
IA_gdb__2_ = "K:\\COUNTY_MUKEY\\IA.gdb"

# Process: Feature Class to Geodatabase (multiple)
arcpy.FeatureClassToGeodatabase_conversion("K:\\SUURGO_Data\\IA\\soil_ia001\\soil_ia001\\spatial\\soilmu_a_ia001.shp;K:\\SUURGO_Data\\IA\\soil_ia003\\soil_ia003\\spatial\\soilmu_a_ia003.shp;K:\\SUURGO_Data\\IA\\soil_ia005\\soil_ia005\\spatial\\soilmu_a_ia005.shp;K:\\SUURGO_Data\\IA\\soil_ia197\\soil_ia197\\spatial\\soilmu_a_ia197.shp;K:\\SUURGO_Data\\IA\\soil_ia053\\soil_ia053\\spatial\\soilmu_a_ia053.shp", IA_gdb__2_)
###############################################################
Tags (2)
0 Kudos
19 Replies
ZiaAhmed
New Contributor III
When I run script after correction, I get error again.
Thanks

import arcpy, os
#
arcpy.env.workspace= "K:/SUURGO_Data/IA"
#
# To create list files from directories and subdirectories
for root, dirs, files in os.walk(arcpy.env.workspace):
    for name in files:       
        if name.startswith("soilmu_a_") and name.endswith(".shp"):
            path = os.path.abspath(os.path.join(root, name))
            # Remove the path from the shapefile name
            base = os.path.basename(path)
            print "Processing "+base
            # CopyFeatures
            arcpy.CopyFeatures_management(path, "K:/COUNTY_MUKEY/IA.gdb"+base.rstrip(".shp"))           
            print arcpy.GetMessages()
#############

Processing soilmu_a_ia001.shp

Traceback (most recent call last):
  File "K:\Script\Python_Script\ImprtFetureGeodatabase_1.py", line 21, in <module>
    arcpy.CopyFeatures_management(path, "K:/COUNTY_MUKEY/IA.gdb"+base.rstrip(".shp"))
  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 K:\COUNTY_MUKEY\IA.shp already exists.
Failed to execute (CopyFeatures).
0 Kudos
RDHarles
Occasional Contributor
Looks like you missed a slash after IA.gdb/

arcpy.CopyFeatures_management(path, "K:/COUNTY_MUKEY/IA.gdb/"+base.rstrip(".shp"))
0 Kudos
ZiaAhmed
New Contributor III
Thank you so much! It is working now. I appreciate your help.
Zia

Looks like you missed a slash after IA.gdb/

arcpy.CopyFeatures_management(path, "K:/COUNTY_MUKEY/IA.gdb/"+base.rstrip(".shp"))
0 Kudos
RDHarles
Occasional Contributor
Glad I could help.
0 Kudos
BC
by
New Contributor II
Any ideas on how to get a list of arcinfo coverages and personal/file geodatabases as well?

Thank you in advance.
0 Kudos
RDHarles
Occasional Contributor
Any ideas on how to get a list of arcinfo coverages and personal/file geodatabases as well?

Thank you in advance.


This is how I get feature classes, tables and datasets inside a file geodatabase:

# For each ws, print the fc's and tbl's.
for ws in arcpy.ListWorkspaces("*", "FileGDB"):    
    arcpy.env.workspace = ws
    # feature classes
    for fc in arcpy.ListFeatureClasses():    
        print fc       
    # tables
    for tbl in arcpy.ListTables():
        print tbl        
    # feature classes in datasets
    for ds in arcpy.ListDatasets():
        arcpy.env.workspace = ds
        for dfc in arcpy.ListFeatureClasses():    
            print dfc
            


Wow, arcinfo coverages, it's been a while.  What do you need to do with them?
0 Kudos
BC
by
New Contributor II
Thanks for your reply.  I am working on a script that would list all .shp and .mdb files on file server into a dbf file.  What I would like is to do is add our old arcinfo coverages on the server to this list.





import arcgisscripting
import sys
import fileinput
import os
import string

gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = 1

dbfFileA = "c:\\d_drive\\temp\\listFC.dbf"

#insert cursor
#
rows = gp.InsertCursor(dbfFileA)

try:
    for root, dirs, files in os.walk("K:/fish/chpp"):
        for file in files:
            if file.endswith(".shp") or file.endswith(".mdb"):
                path = os.path.abspath(os.path.join(root, file))
                #print path
                row = rows.NewRow()
                row.Name = os.path.basename(path)
                row.Location = path
                rows.InsertRow(row)

    del rows
    print "successful"
except:
    print "Error"
    print gp.getmessages()
     


0 Kudos
RDHarles
Occasional Contributor
It's not pretty, but I've used the following code to identify coverages in the past.
A coverage is nothing more that a folder/directory with a bunch of "coverage files" inside.

for dir in os.listdir(''):
    if os.path.isdir(dir):
        # prj.adf seem to occur in every "coverage".
        if os.path.exists(dir+"/prj.adf"):            
            print " Creating "+dir+".e00 from "+dir+"..."
            gp.Export_arc("COVER", dir, dir+".e00")
            gp.GetMessages()
0 Kudos
BC
by
New Contributor II
Thank you!
Here's my final script:
import arcgisscripting
import sys
import fileinput
import os
import string

gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = 1

dbfFileA = "c:\\d_drive\\temp\\listFC.dbf"

#insert cursor
#
rows = gp.InsertCursor(dbfFileA) 

try:
    for root, dirs, files in os.walk("C:/D_Drive/temp"):
        for file in files:
            if file.endswith(".shp"):
                #print "a"
                path = os.path.abspath(os.path.join(root, file))
                #print path
                row = rows.NewRow()
                row.Name = os.path.basename(path)
                row.Location = path
                row.Directory = os.path.basename(os.path.dirname(path))
                row.Type = "Shapefile"
                rows.InsertRow(row)
            elif file.endswith("gdb"):
                #print "1"
                path = os.path.abspath(os.path.join(root, file))
                #print path
                row = rows.NewRow()
                #row.Name = os.path.dirname(path)
                row.Name = os.path.basename(os.path.dirname(path))
                #row.Name = "gdb"
                row.Location = os.path.dirname(path)
                row.Directory = os.path.basename(os.path.dirname(path))
                row.Type = "File Geodatabase"
                rows.InsertRow(row)
            elif file.endswith(".mdb"):
                #print "3"
                path = os.path.abspath(os.path.join(root, file))
                #print path
                row = rows.NewRow()
                row.Name = os.path.basename(path)
                row.Location = path
                row.Directory = os.path.basename(os.path.dirname(path))
                row.Type = "Personal Geodatabase"
                rows.InsertRow(row)
            elif file.endswith("prj.adf"):
                #print "4"
                path = os.path.abspath(os.path.join(root, file))
                row = rows.NewRow()
                row.Name = os.path.basename(os.path.dirname(path))
                row.Location = os.path.dirname(path)
                row.Directory = os.path.basename(os.path.dirname(path))
                row.Type = "ArcInfo Coverage"
                rows.InsertRow(row)
    del rows
    print "successful"
except:
    print "Error"
    print gp.getmessages()
0 Kudos
ToddMcDonnell
New Contributor II
I need to run flow accumulation on many GRID files that are contained in different folders using ArcGIS 10. Does anyone have suggestions for a base set of code I could begin to work with?
0 Kudos