I'm attempting to convert a number of shapefiles into a geodatabase and automate the process using python. The directories that contain the shapefiles that need to be converted are named by county for example "cass". The geodatabases are named after the counties as well but end in the extension .gdb and they end up looking like this "cass.gdb". I've already tried using the os.walk function for both directories and comparing them in order to put the correct files in the correct place but I simply end up with a loop that only compares one county file to one county .gdb without moving to the next item. I was wondering if anyone would know how to correct this problem, or have a more efficient way of doing it. Here is my code as it sits right now.

import arcpy
import os

inFile = "P://County_Map_Book//counties"
outGeoPath = "P://County_Map_Book//mapbookCountiesGeodatabases"
doubSlash = "//"

#This compound loop structure walks
#through directories of shapefiles
for root, dirs, files in os.walk(inFile):
    for compare in dirs:
        compareShapes = compare

for root, dirs,files in os.walk(outGeoPath):
    for compareGD in dirs:
        if compareGD.endswith(".gdb"):
            compareGDB = compareGD[:-4]

for comp in compareShapes:
    if comp in compareGDB:
        print compareGDB + " shares name with " + compareShapes
  • LOop inside Files instead of Dirs. According to os. Walk doc tutorialspoint.com/python/os_walk.htm – Hugo Roussaffa - GeoDatup Oct 16 '17 at 20:54
  • You seem to be stuck on the os.walk and although you import ArcPy your code snippet does not use it. Consequently I think this is a pure Python question that would be better researched/asked at Stack Overflow. – PolyGeo Oct 16 '17 at 21:52
  • This answer is a little brief, can you show the OP how to apply the docs within their os.walk and perhaps compare with arcpy.da.walk considering the script imports arcpy to give some volume and weight. I think the iteration of dirs is correct in the second loop because they're looking for file geodatabase, I would use os.path.splitext(os.path.basename(compareGD))[1].lower() == '.gdb' but endswith works fine here too. – Michael Stimson Oct 16 '17 at 22:03

I found a solution! Admittedly it's not the most elegant as it doesn't compare the two file structures but rather it concatenates the output pathname using the input pathname that is provided via the os.walk function. As mentioned before I had my shapfiles in directories named after counties so they had file names looked like this; "barnes". The directories that held the geodatabases had filenames that looked like this; "barnesGeodatabase", and of course the actual geodatabase looks like this "barnes.gdb". Using os.walk got the name of the directory that held the original shapefile ex. "barnes" then added that to an output pathname. Following that walked the files in that directory and got their names ex. "RailLinesbarnes.shp". From this point it was easy to concatenate the output pathname together. My final block of code is listed below.

import arcpy
import os
arcpy.env.overwriteOutput = True

inFile = "P://County_Map_Book//counties"
outGeoPath = "P://County_Map_Book//mapbookCountiesGeodatabases"
doubSlash = "//"
gdb = ".gdb"
geodatabase = "Geodatabase"

for root, dirs, files in os.walk(inFile):
    for counties in dirs:
        newPath = inFile + doubSlash + counties
        for files, files, files in os.walk(newPath):
            for countFiles in files:
                if countFiles.endswith(".shp"):
                    newName = countFiles[:-4] + "New"
                    outPath = outGeoPath + doubSlash + counties + geodatabase + doubSlash + counties + gdb
                    inPath = inFile + doubSlash + counties + doubSlash + countFiles
                    print newName
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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