0

I am trying to write a python script that will find all raster files in my server including 'GRID'.

Here is my sample script:

import os
import arcpy
path = r"F:\Geodaten"
for root, dirs, files in os.walk(path):
    #print ("we are in " + dirs)
    #Accessing to directories...................................

    #accessing files...........................
    for fileName in files:
        basename, extension = os.path.splitext(fileName)
        if (extension == ".jpg") or (extension == ".tif"):
            fullPath = os.path.join(root, fileName)
            #mxd = arcpy.mapping.MapDocument(fullPath)
            #print ("we are in " + dirs)
            print "bilder: " + fullPath
walk = arcpy.da.Walk(path, type="GRID")

for root, dirs, files in walk:
    for filename in files:

        if "." not in filename:
            fullPath=os.path.join(root, filename)
            print "Grid: " + fullPath


print("......searching Done...............")

my result is showing some unexpected grid files that I could not access like this:

Grid: F:\Geodaten\Eigene\GPS\Laukesa2008\shp\20080529.shp\20080529
Grid: F:\Geodaten\Eigene\GPS\Laukesa2008\shp\20080530.shp\20080530
Grid: F:\Geodaten\Eigene\GPS\Laukesa2008\shp\20080531.shp\20080531
Grid: F:\Geodaten\Eigene\GPS\Laukesa2008\shp\20080602.shp\20080602
Grid: F:\Geodaten\Eigene\GPS\Laukesa2008\shp\20080603.shp\20080603
Grid: F:\Geodaten\Eigene\GPS\Laukesa2008\shp\20080604.shp\20080604
Grid: F:\Geodaten\Eigene\GPS\Laukesa2008\shp\20080605.shp\20080605

How could I avoid unexpected results?

3

Now I have a solution to my own question. I have used arcpy.ListRasters() function to identify correct GRID raster. I would like to share my solution as follows:

import os
import arcpy
path = r"F:\Geodaten"
for root, dirs, files in os.walk(path):
    #accessing files...........................
    for fileName in files:
        basename, extension = os.path.splitext(fileName)
        if (extension == ".jpg") or (extension == ".tif"):
            fullPath = os.path.join(root, fileName)
            print "bilder: " + fullPath
    for d in dirs:
        fulPath=os.path.join(root,d)
        arcpy.env.workspace=fulPath #definig every sub folder in directory as workspace
        # searching for 'GRID' raster in workspace       
        rasterlist = arcpy.ListRasters("*","GRID")
        if len(rasterlist)>=0:              #  verifying list is not empty
            for r in rasterlist:
                fullPath=os.path.join(root,r)
                print "Grid: " + fullPath # gaining path for each raster
0

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.