Question

I want the user to process files in 2 different folders. The user does by selecting a folder for First_Directory and another folder for Second_Directory. Each of these are defined, have their own algorithms and work fine if only one directory is selected at a time. If the user selects both, only the First_Directory is processed.

Both also contain the glob module as shown in the simplified code which I think the problem lies. My question is: can the glob module be used multiple times and if not, is there an alternative?

##Test=name
##First_Directory=folder
##Second_Directory=folder

path_1 = First_Directory
path_2 = Second_Directory
path = path_1 or path_2

os.chdir(path)

def First(path_1):
output_1 = glob.glob('./*.shp')
#Do some processing

def Second(path_2):
output_2 = glob.glob('./*.shp')
#Do some other processing

if path_1 and path_2:
    First(path_1)
    Second(path_2)
elif path_1:
    First(path_1)
elif path_2:
   Second(path_2)
else:
    pass
Was it helpful?

Solution

You can modify your function to only look for .shp files in the path of interest. Then you can use that function for one path or both.

def globFolder(path):
    output_1 = glob.glob(path + '\*.shp')

path1 = "C:\folder\data1"
path2 = "C:\folder\data2"

Then you can use that generic function:

totalResults = globFolder(path1) + globFolder(path2)

This will combine both lists.

OTHER TIPS

I think by restructring your code can obtain your goal:

def First(path,check):

  if check:

     output = glob.glob(path+'./*.shp')
  #Do some processing
  else:
     output = glob.glob(path+'./*.shp')
#Do some other processing
 return output
#
#
#
 First(path_1,True)
 First(path_2,False)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top