Reputation: 298
I've one root folder under which there are two folders, now I want to sort all files under root folder according to their names irrespective of sub-folder names.
Below is code which I tried, but problem here is that is sorting according to the name of subfolder under which the files are :
.../verify/AU1/APPLaunch_ftrace_au1.txt,
.../verify/AU1/Mp3BT_ftrace_au1.txt,
.../verify/AU2/APPLaunch_ftrace_au2.txt,
.../verify/AU2/Mp3BT_ftrace_au2.txt
files_list = []
for root, dirs, files in os.walk(trace_folder, topdown = False):
files_list.extend(join(root,f) for f in files)
files_list.sort()
what I would like to have is :
.../verify/AU1/APPLaunch_ftrace_au1.txt,
.../verify/AU2/APPLaunch_ftrace_au2.txt,
.../verify/AU1/Mp3BT_ftrace_au1.txt,
.../verify/AU2/Mp3BT_ftrace_au2.txt
Upvotes: 1
Views: 902
Reputation: 140168
just add a sort criteria to sort
which only considers the basename of the file
files_list.sort(key=os.path.basename)
if you don't care about casing, that's also doable:
files_list.sort(key=lambda x : os.path.basename(x).lower())
Upvotes: 1