13

I have a script which scans a directory and outputs basic raster data information such as the file name, format, number of bands, and etc. I need a way to make it so if the directory does not contain raster data (i.e., anything other than raster data), a message is displayed stating that the directory doesn't have the correct data type.

I know ArcPy has a Describe() function that I could use to determine the type of data in a folder, but am not sure how to implement it. This is what I have so far:

rasterList = arcpy.ListRasters("*", "ALL")
filesType = arcpy.DataType('RasterDataset') # Can use `DatasetType` as well. 
 # I've tested this function to describe
 # raster data and ArcPy prints out
 # 'RasterDataset', that is why I have it 
 # there in the brackets.
for name in rasterList:
 if rasterList == filesType:
 print ("\nFilename:"), name
 else:
 print ("This directory does not contain any raster data.")

Any suggestions?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 19, 2011 at 18:58
0

1 Answer 1

16

How about something simple like:

if len(rasterList) == 0:
 print ("This directory does not contain any raster data.")
else:
 # Your raster processing code

The len() function calculates the length of the returned string/list, so if it returns 0 then you know nothing in the folder matched the criterion (in this case, being a raster). This way, if the folder contains any rasters (even if not every file is a raster) they will be processed.

answered Nov 19, 2011 at 22:30
1
  • Thanks nmpeterson! That was it. I knew I was missing something simple. Can't believe I didn't think of the len() function. Commented Nov 19, 2011 at 23:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.