题
我希望用户在2个不同的文件夹中处理文件。用户通过为 first_directory 和另一个文件夹来选择一个文件夹来做,以及 second_directory 。这些都定义了,如果一次只选择一个目录,则拥有自己的算法并工作正常。如果用户选择两者,则仅处理 first_directory 。
两个也包含如简化代码所示的Glob模块,我认为问题谎言。我的问题是:可以多次使用Glob模块,如果没有,还有替代吗?
##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
. 解决方案
您可以修改您的函数,只能在感兴趣的路径中查找生成的.shp
文件。然后,您可以将该函数用于一条路径或两者。
def globFolder(path):
output_1 = glob.glob(path + '\*.shp')
path1 = "C:\folder\data1"
path2 = "C:\folder\data2"
.
然后,您可以使用该通用功能:
totalResults = globFolder(path1) + globFolder(path2)
.
这将组合两个列表。
其他提示
我认为通过重组您的代码可以获得您的目标:
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)
. 不隶属于 StackOverflow