7

I'm currently trying to build two lists containing folder names, in order to use their elements for geoprocessing tools later on. However I struggle with creating the lists. I'm using ArcGIS 10.3 (Python 2.7).

What I need:

C:\test contains multiple subfolders that I would like to put in the first list, i.e. C:\test\test1, C:\test\test2, etc. The first set of subfolders contains another set, i.e. C:\test\test1\xy1, C:\test\test1\xy2 etc. What I need is a list with the names, not the paths, to each of the first level subfolders as in firstlevelList = [test1, test2, test3] and secondlevelList = [xy1, xy2, xy3]. I then want to use the arcpy.MosaicToNewRaster_management function to process data in each of the secondlevelList separetely (sort of according to this question).

What I've tried:

import arcpy, os

workspace = r"C:\test"
arcpy.env.workspace = workspace
arcpy.env.scratchworkspace = workspace
arcpy.env.overwriteOutput = True


firstlevelList = []
firstlevelList.append(os.listdir(workspace))
## up to here, everything works very well, the list contains all the available subfolders in my workspace.

secondlevelList = []
for element in firstlevelList:
    newPath = """{0}\{1}""".format(workspace, element)
    secondlevelList.append(os.listdir(newPath))

## This creates the following error:
##Runtime error
##Traceback (most recent call last):
##  File "<string>", line 8, in <module>
##WindowsError: [Error 3] Das System kann den angegebenen Pfad nicht finden: "C:\\Users\\sutter\\Desktop\\GIS_temp\\test\\['test1', 'test2', 'test3']/*.*"

I understand that apparently I cannot use a list element to create a new path? I then also tried to following, which worked for one element of the fistlevelList() but not for all of them... :

secondlevelList = []
i = 0
for element in firstlevelList:
     newPath = """{0}\{1}""".format(workspace, element[i])
     secondlevelList.append(os.listdir(newPath))
     i += 1

Am I using the os.listdir function the wrong way? My knowledge about the os.path / os.walk functions is rather limited but I guess they could also help? Would I be better off using the arcpy.ListFiles() function?

The aim of the script: Finally I would like to create a mosaic for all rasters in the secondlevelList separately. Example: folder C:\test\test1\xy1 contains 3 rasters. They need to be mosaiced. Then the following folder needs to be processed C:\test\test1\xy2 etc. I wrote this code for doing this:

for tile in firstlevelList:
    for run in secondlevelList:
        zeitschrittRaster = arcpy.ListRasters("*", "TIF")
        arcpy.MosaicToNewRaster_management(zeitschrittRaster, Sammelordner, """{0}.tif""".format(run), "", "32_BIT_FLOAT", 10, 1, "MAXIMUM", "FIRST")
2
  • 2
    Is there any particular reason why you need only the folder names instead of full paths? The Mosaic to New Raster accepts the full paths to the datasets. Commented Aug 18, 2016 at 10:23
  • @AlexTereshenkov: thanks for your comment. You are right - then my sencondlevelList could also contain paths. I'll quickly edit my question and add the code for the mosaic to new raster.
    – dru87
    Commented Aug 18, 2016 at 11:15

2 Answers 2

7

There are dedicated list functions in arcpy and I would use them rather than the python os.listdir function:

import arcpy, os

workspace = r"C:\test"
arcpy.env.workspace = workspace
arcpy.env.scratchworkspace = workspace
arcpy.env.overwriteOutput = True

firstlevelList = arcpy.ListWorkspaces(workspace_type = "Folder")

for element in firstlevelList:
    arcpy.env.workspace = element
    secondlevelList = arcpy.ListFiles()
    for file in secondlevelList:
        # do something

file represents the file itself even though if you print it you will only get its name. You can input it to a tool without having to recreate the full path to the file. Nevertheless if you want to do this for some reason, add this line under the last for block:

path_to_file = os.path.join(ws, file)

Depending on the type of the files (rasters?) and what you want to do with them, another list function might be more approriate:

See also the help page for arcpy.ListWorkspaces() and arcpy.ListFiles().

2
  • Great answer, thanks a lot! I easily managed to put the list together like this by slightly modifing your code.
    – dru87
    Commented Aug 18, 2016 at 11:41
  • Great! Glad I could help.
    – GISGe
    Commented Aug 18, 2016 at 11:44
1

Maybe this can help you: Using Python to execute merge of rasters

I used os.path.join and os.walk (os path)

Your Answer

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

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