Search code examples
pythonarcpy

trouble with FeatureClassToGeodatabase_conversion in arcpy, using .da.Walk


With this code, I am trying to read all files a directory and all its subdirectories. I have another list of files names, if the search finds files in the directories that are on the other list, I want to copy those feature classes to another location. When the code gets to FeatureClasstoGeodatabase, I keep getting an error that the input features data type is not supported or does not exist. I wasn't sure if I needed to somehow get the path as well as the filename so I created a couple of lists to capture that separately, but I'm kind of stuck here:

import arcpy
import os
workspace = r'F:\SF_HMP - transferred to Ydrive'
output_loc = r'C:\temp\temp.gdb'
mssng_files = r'F:\SF_HMP - transferred to Ydrive\Maps\broken_links_missing_files.txt'
files_to_find = []
layers_list = []
layers_path = []

with open(mssng_files) as filelist:
  for line in filelist:
    files_to_find.append(line.strip())

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,datatype="FeatureClass"):
  for filename in filenames:
    layers_list.append(filename)
    layers_path.append(os.path.join(dirpath,filename))
  for lyr in layers_list:
    if lyr in files_to_find:
        arcpy.FeatureClassToGeodatabase_conversion(lyr,output_loc)

Solution

  • I realized I needed to specify the workspace for each file to be copied over. I also repeated the code to search for and copy over rasters and tables:

    import arcpy,os, easygui,sys
    
    mssng_files = r'L:\SF_HMP - transferred to Ydrive\Maps\broken_links_missing_files.txt'
    wkspc = easygui.enterbox("Enter workspace path:",title='Search for Files')
    output_loc = easygui.enterbox("Output location:",title='Copy Files')
    
    with open(mssng_files) as filelist:
      for line in filelist:
        files_to_find.append(line.strip())
    
    
    for dirpath, dirnames, filenames in arcpy.da.Walk(wkspc,datatype='FeatureClass'):
      for filename in filenames:
            if filename in files_to_find:
                ws_l = os.path.join(dirpath,filename)
                arcpy.env.workspace = ws_l
                arcpy.FeatureClassToGeodatabase_conversion(ws_l,output_loc)
    
    for dirpath, dirnames, filenames in arcpy.da.Walk(wkspc,datatype='RasterDataset'):
      for filename in filenames:
            if filename in files_to_find:
                ws_r = os.path.join(dirpath,filename)
                arcpy.env.workspace = ws_r
                arcpy.RasterToGeodatabase_conversion(ws_r,output_loc)
    
    for dirpath, dirnames, filenames in arcpy.da.Walk(wkspc,datatype='Table'):
      for filename in filenames:
            if filename in files_to_find:
                ws_t = os.path.join(dirpath,filename)
                arcpy.env.workspace = ws_t
                arcpy.TableToGeodatabase_conversion(ws_t,output_loc)