Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get list of all file names in a directory with its original in folder order

I want to get the list of all file names in a directory but the order of files matters to me. (And I’m using Google Colab if that matters)


P.S: I’ve accepted the first answer, but just in case know that when I
moved my files from google drive to my local PC and run the code, it
worked and printed those with the original order. I think there is
something with the Google Colab


Suppose my files are in this order in my directory:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

file1.txt
file2.txt
file3.txt
...
filen.txt

P.S: These file names are just examples to be clear. In reality my
filenames don’t have any pattern to be sorted, they are like
1651768933554-b6b4deb3-f245-4763-b38d-71a4b19ca948.wav

When I get the file names via this code:

import glob
for g in glob.glob(MyPath):
  print(g)

I’ll get something like this:

file24.txt
file5.txt
file61.txt
...

I want to get the file names in the same order they are in the folder.

I searched as far as I could but I didn’t find any SO post solving my problem. Appreciate any help.

P.S: I tried from os import listdir too but didn’t work.

>Solution :

The way they exist in the folder isn’t really a thing. In reality they are all sitting somewhere in memory and not necessarily next to each other in any way. The GUI usually displays them in alphabetical order, but there are also usually filters you can click to sort them in whatever way you choose.

The reason they are printing that way is because it is alphabetical order. Alphabetically 2 comes before 5 so 24 comes before 5.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading