How to sort os.walk list? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to sort os.walk list? (/thread-30142.html) |
How to sort os.walk list? - Denial - Oct-09-2020 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: 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 RE: How to sort os.walk list? - DPaul - Oct-09-2020 What is the logic behind your sorting order ? Individual files first, then the first subdir only...? Paul RE: How to sort os.walk list? - Denial - Oct-09-2020 (Oct-09-2020, 07:39 AM)DPaul Wrote: What is the logic behind your sorting order ? 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. RE: How to sort os.walk list? - ibreeden - Oct-09-2020 for filename in sorted(files): RE: How to sort os.walk list? - Denial - Oct-09-2020 (Oct-09-2020, 10:34 AM)ibreeden Wrote:for filename in sorted(files): Damn!!! This is so good and easy. Thanks RE: How to sort os.walk list? - bowlofred - Oct-09-2020 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. RE: How to sort os.walk list? - Denial - Oct-10-2020 (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. Thanks. Now all my file and directories are sorted in perfect way. |