1

I run this variable that contains the directories where the script opens the files that exist there but the files appear out of order:

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']

It used to show them fine (alphabetically) since testa is before tastb but now even though the date modified in the folder doesn't seem to play a role after checking it in the folder.

I don't know what to do to make them appear always: first the testa and then the testb files. I thought that by placing the 'a' after test and 'b' for the next it would ensure that the order would be like that. But it's not like that apparently.

Info: if you want to know how the paths are collected, it's from a script that reads them from a main folder. It used to show them correctly but now it doesn't.

script that gets the files

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]

1 Answers1

2

The answer is to take the list and sort it. By doing it like this, any order the list may have initially, will change to the ideal sorted result.

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']