Python 更改路径中文件的显示顺序?

Python 更改路径中文件的显示顺序?,python,directory,Python,Directory,我运行此变量,该变量包含脚本在其中打开存在的文件但文件出现顺序错误的目录: path_files ['C:\\Users\\user\\Desktop\\AUT\\testb\\AST\\AST.shp', 'C:\\Users\\user\\Desktop\\AUT\\testb\\PSA\\PSA.shp', 'C:\\Users\\user\\Desktop\\AUT\\testa\\AST\\AST.shp', 'C:\\Users\\user\\Desktop\\AUT\\te

我运行此变量,该变量包含脚本在其中打开存在的文件但文件出现顺序错误的目录:

path_files

['C:\\Users\\user\\Desktop\\AUT\\testb\\AST\\AST.shp',
 'C:\\Users\\user\\Desktop\\AUT\\testb\\PSA\\PSA.shp',
 'C:\\Users\\user\\Desktop\\AUT\\testa\\AST\\AST.shp',
 'C:\\Users\\user\\Desktop\\AUT\\testa\\PSA\\PSA.shp']
因为
testa
tastb
但是现在,即使在文件夹中检查日期后,在文件夹中修改的日期似乎没有起作用

我不知道如何使它们始终出现:首先是
testa
,然后是
testb
文件。我认为,通过在测试之后放置“a”,在下一个测试之后放置“b”,可以确保顺序是这样的。但显然不是这样

信息:如果您想知道路径是如何收集的,可以从从主文件夹读取路径的脚本中获取。以前它能正确地显示它们,但现在不行了

获取文件的脚本

def splitpath(path):
folders = []
while 1:
    path, folder = os.path.split(path)

    if folder != "":
        folders.append(folder)
    else:
        if path != "":
            folders.append(path)

        break
folders.reverse()
return folders

rootfolder = r'C:\Users\user\Desktop\AUT'
shapelist = []
for path, subdirs, files in os.walk(rootfolder):
    for name in files:
        if name.endswith('.shp'):
            shapelist.append(os.path.join(path, name))

#List all subfolders
subfolders = set(map(lambda x:splitpath(x)[-3], shapelist))
#Create list of sublists where each sublist are the shapefiles in that subfolder
grouped_shapefiles = [[y for y in shapelist if splitpath(y)[-3]==x] for x in subfolders]

答案是把清单拿出来分类。通过这样做,列表最初可能具有的任何顺序都将更改为理想的排序结果

path_files.sort()
path_files

['C:\\Users\\user\\Desktop\\AUT\\testa\\AST\\AST.shp',
 'C:\\Users\\user\\Desktop\\AUT\\testa\\PSA\\PSA.shp',
 'C:\\Users\\user\\Desktop\\AUT\\testb\\AST\\AST.shp',
 'C:\\Users\\user\\Desktop\\AUT\\testb\\PSA\\PSA.shp']

请回答您的问题,并包括生成
路径\u文件的代码。
。欢迎使用堆栈溢出!您能否提供更多信息,例如收集这些路径的脚本中的代码?是否要对列表进行排序?@ThatBird这可能是一个解决方案,可能与