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")