Use the os
module, get the extension of a file using os.path.splitext
and then use list.sort
.
import os
files = os.listdir(path)
def func(x):
return os.path.splitext(x)[::-1]
files.sort(key = func)
Demo:
>>> lis = ['file1.zip', 'file2.zip', 'inotify.c', 'cmpsource.c', 'myfile.h']
>>> def func(x):
return os.path.splitext(x)[::-1]
>>> lis.sort(key = func)
>>> lis
['cmpsource.c', 'inotify.c', 'myfile.h', 'file1.zip', 'file2.zip']