Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to sort os.walk list?
#1
Hello All

How to sort contents of the list I gathered from os.walk() method?

Here is my code:
#!/usr/bin/env python3                                                           
                                                                                 
import os                                                                        
                                                                                 
EXTN = ['.avi', '.flv', '.mkv', '.mov', '.mp4', '.mpeg', '.mpg', '.webm', '.wmv']
                                                                                 
videos_list = []                                                                 
                                                                                 
for root, dirs, files in os.walk('.'):                                           
    for filename in files:                                                       
        for ext in EXTN:                                                         
            if filename.lower().endswith(ext.lower()):                           
                videos_list.append(os.path.join(root, filename))                 
                                                                                 
for i in videos_list:                                                            
        print(i)                         
And here is its output:
Output:
./Binary to small.mp4 ./01 Course Welcome.mp4 ./Tutorial.webm ./Intro.mp4 ./Audience.mp4 ./Binary to CAPITAL.MP4 ./Intros.mp4 ./_K24223951.mkv ./03 Using The Command Line.mp4 ./Beta/Decimal to binary conversion.mp4 ./Beta/Decimal to octal conversion.mp4 ./Beta/beta1/Will Data Science be on Demand.mp4 ./Beta/beta1/What is Data Science.mp4 ./Beta/beta1/Data Science Roles.mp4 ./Beta/beta1/Why do we need Data Science.mp4 ./Alpha/Introduction to numbers.mp4 ./Alpha/Course introduction.mp4 ./Theta/4. The Interface.mp4 ./Theta/1. Introduction.mp4 ./Theta/3. The Study Plan.mp4 ./Theta/theta1/1. Organization of Job Search.mp4 ./Theta/theta1/2. Job Search Sites.mp4 ./Theta/theta1/theta2/01 - List, Create and Delete Partitions on MBR and GPT Disks.mp4 ./Theta/theta1/theta2/03 - Configure Systems to Mount File Systems at Boot by UUID or Label.mp4 ./Theta/theta1/theta2/04 - Add New Partitions and Logical Volumes and Swap to a System Non-Destructively.mp4 ./Theta/theta1/theta2/02 - Create, Assign Physical Volumes to Volume Groups and Create and Delete Logical Volumes.mp4
Please note that the files within directory tree are not sorted. I want output be something like this.

./01 Course Welcome.mp4
./03 Using The Command Line.mp4
./Audience.mp4
./Binary to CAPITAL.MP4
./Binary to small.mp4
./Intro.mp4
./Intros.mp4
./_K24223951.mkv
./Tutorial.webm

./Alpha/Course introduction.mp4
./Alpha/Introduction to numbers.mp4

./Beta/Decimal to binary conversion.mp4
./Beta/Decimal to octal conversion.mp4

./Beta/beta1/Data Science Roles.mp4
./Beta/beta1/What is Data Science.mp4
./Beta/beta1/Why do we need Data Science.mp4
./Beta/beta1/Will Data Science be on Demand.mp4

./Theta/1. Introduction.mp4
./Theta/3. The Study Plan.mp4
./Theta/4. The Interface.mp4

./Theta/theta1/1. Organization of Job Search.mp4
./Theta/theta1/2. Job Search Sites.mp4

./Theta/theta1/theta2/01 - List, Create and Delete Partitions on MBR and GPT Disks.mp4
./Theta/theta1/theta2/02 - Create, Assign Physical Volumes to Volume Groups and Create and Delete Logical Volumes.mp4
./Theta/theta1/theta2/03 - Configure Systems to Mount File Systems at Boot by UUID or Label.mp4
./Theta/theta1/theta2/04 - Add New Partitions and Logical Volumes and Swap to a System Non-Destructively.mp4


Is this possible? Or am I expecting too much?

Thanks
Reply
#2
What is the logic behind your sorting order ?
Individual files first, then the first subdir only...?
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
(Oct-09-2020, 07:39 AM)DPaul Wrote: What is the logic behind your sorting order ?
Individual files first, then the first subdir only...?
Paul

Sort of. In one sentence I can say - 'Sort files in their respective directory.' Doest matter if directries themselves sorted or not, but files within them should be sorted.
Reply
#4
for filename in sorted(files):
Reply
#5
(Oct-09-2020, 10:34 AM)ibreeden Wrote:
for filename in sorted(files):

Damn!!! This is so good and easy. Dance Thanks
Reply
#6
If you want to visit the directories in order as well, you can also sort, but you have to sort the existing list, not return a new one.

Adding
dirs.sort()
after the os.walk() call will sort the directories and then the processing of them will be in sorted order as well.
Reply
#7
(Oct-09-2020, 03:27 PM)bowlofred Wrote: If you want to visit the directories in order as well, you can also sort, but you have to sort the existing list, not return a new one.

Adding
dirs.sort()
after the os.walk() call will sort the directories and then the processing of them will be in sorted order as well.

Thanks. Now all my file and directories are sorted in perfect way.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list.sort() returning None SmallCoder14 8 403 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
Smile Python & MSGraph - Learning to Walk ITMan020324 2 354 Feb-04-2024, 04:37 PM
Last Post: ITMan020324
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,277 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,000 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  EasySNMP Walk/BulkWalk pylance 3 2,031 Nov-29-2021, 12:00 PM
Last Post: pylance
  [solved] Sort list paul18fr 5 2,800 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  Sort List of Lists by Column Nju 1 10,045 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  os.walk question DPaul 2 2,281 May-31-2020, 02:08 PM
Last Post: DPaul
  os.walk(Path("path_string")) giving error Kumarkv 4 3,794 May-10-2020, 08:46 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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