Python Forum
How can I sort my names of files in for loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I sort my names of files in for loop?
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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) 
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through all files in a directory? Winfried 10 360 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  File loop curiously skipping files - FIXED mbk34 10 814 Feb-10-2024, 07:08 AM
Last Post: buran
  How to loop through all excel files and sheets in folder jadelola 1 4,510 Dec-01-2022, 06:12 PM
Last Post: deanhystad
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,324 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Using Python to loop csv files to open them Secret 4 2,741 Sep-13-2020, 11:30 AM
Last Post: Askic
  How to sort image files according to a metadata file? Brahmslove 1 3,138 Dec-05-2019, 11:25 PM
Last Post: scidam
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 5,723 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  Loop through folder of Excel Files and extract single column fioranosnake 2 4,536 Oct-28-2019, 05:19 PM
Last Post: fioranosnake
  Loop files - Extract List Data To Individual Columns in CSV dj99 5 3,284 May-19-2019, 10:29 AM
Last Post: dj99
  Delete Lines that Contain Words - Loop through files in a folder - Write to new files dj99 3 3,452 May-18-2019, 06:34 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020