S
S
Sergey Karbivnichy2022-01-10 20:56:14
Python
Sergey Karbivnichy, 2022-01-10 20:56:14

How to sort the list of files?

There is a list of files. How can I sort it by the number in the name? Now like this:

import glob

file_list = glob.glob('startandroid/test/*.html')

new_file_list = [x.split('/')[-1] for x in file_list]

for x in sorted(new_file_list):
  print(x)

At the output I have:
Урок 1. Введение..html
Урок 10. Оптимизируем реализацию обработчиков..html
Урок 100. Service. IntentService. Foreground. Автозагрузка сервиса.html
Урок 101. Создаем свой ContentProvider.html
Урок 102. Touch – обработка касания.html
Урок 103. MultiTouch – обработка множественных касаний.html
Урок 104. Android 3. Fragments. Lifecycle.html
Урок 105. Android 3. Fragments. Динамическая работа.html
Урок 106. Android 3. Fragments. Взаимодействие с Activity.html
Урок 107. Android 3. ActionBar. Размещение элементов.html
Урок 108. Android 3. ActionBar. Навигация - табы и выпадающий список.html
Урок 109. Android 3. Fragments. ListFragment - список.html
Урок 11. Папка res_values. Используем ресурсы приложения..html
Урок 110. Android 3. Fragments. DialogFragment - диалог.html
...

I need to make it so that it was: Lesson 1..., Lesson 2..., Lesson 3... etc.

Has anyone come across this, or do you have a snippet for a similar task?

Almost all file managers support such sorting, maybe Python has it too?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2022-01-10
@hottabxp

On the knee, you can do this: there is another module
sorted(new_file_list, key=lambda n: n.split()[1])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question