Python Forum

Full Version: How can I sort my names of files in for loop?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have some files. I am running my script.

import os

list_files = os.listdir('.')
for x in list_files:
    print(x)
output:
[DownSub.com] Beginner Levels - Lesson 13 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 20 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 11 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 12 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 14 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 21 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 27 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 23 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 22 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 26 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 6 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 8 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 1 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 18 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 9 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 5 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 3 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 15 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 16 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 17 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 2 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 10 - Practical English Drama.srt
[DownSub.com] Beginner Levels - Lesson 7 - Practical English Drama.srt
Normally I would use the re module to make a regular expression to pull out the digits, and convert them with int(). Put that in a number and you can sort by lesson number using the key parameter of the sort method.

In this case I note that all the numbers are in the same place. Find the slice of the first string that gives you '10'. That will give you the lesson number for each file. The last one will be '7 ', but int can handle the space without choking. So you can do what I described above, but with a string slice instead of a regular expression.
Why to use for-loop?

os.listdir() returns list of filenames. There are built-in function sorted() and list method .sort for sorting.

One can write helper function (or use lambda) used as key in sorting. Only digits in filename are the ones you want to use for sorting, so you extract digits from string and convert resulting string to int and use this as key:

>>> def by_numbers(s): 
>>>     return int(''.join(char for char in s if char.isdigit()))
...
>>> sorted(list_files, key=by_numbers)