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


Messages In This Thread
How to sort os.walk list? - by Denial - Oct-09-2020, 06:52 AM
RE: How to sort os.walk list? - by DPaul - Oct-09-2020, 07:39 AM
RE: How to sort os.walk list? - by Denial - Oct-09-2020, 10:16 AM
RE: How to sort os.walk list? - by ibreeden - Oct-09-2020, 10:34 AM
RE: How to sort os.walk list? - by Denial - Oct-09-2020, 10:38 AM
RE: How to sort os.walk list? - by bowlofred - Oct-09-2020, 03:27 PM
RE: How to sort os.walk list? - by Denial - Oct-10-2020, 05:28 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 597 Apr-29-2024, 04:38 PM
Last Post: Calab
  list.sort() returning None SmallCoder14 8 644 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
Smile Python & MSGraph - Learning to Walk ITMan020324 2 435 Feb-04-2024, 04:37 PM
Last Post: ITMan020324
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,345 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,192 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  EasySNMP Walk/BulkWalk pylance 3 2,115 Nov-29-2021, 12:00 PM
Last Post: pylance
  [solved] Sort list paul18fr 5 2,932 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  Sort List of Lists by Column Nju 1 11,955 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  os.walk question DPaul 2 2,358 May-31-2020, 02:08 PM
Last Post: DPaul
  os.walk(Path("path_string")) giving error Kumarkv 4 3,902 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