-1

I try since a while to get all names of files with a certain extension (let's say .txt) in a folder. I found a lot of close topics online but never what I wanted to do.

The closest I've reach is by using:

listFiles = glob.glob(sourdir + '/*.txt')
or
files = fnmatch.filter(os.listdir(sourdir+"/"), "*.txt")
or
[s for s in os.listdir(sourdir+"/") if s.endswith('.txt')]

which gives me all the .txt files in a certain folder. HOWEVER, it automatically sort the files ! In my folder I have :

file0.txt, file1.txt, file2.txt, file3.txt, ...

And with anything that give me those files I have something like :

file0.txt, file1.txt, file10.txt, file11.txt, ...

I just want to read the name of the file in the same order in which they are in the folder ! How is it so hard to do simple things..

If I do os.listdir(sourdir+"/")[i] in a loop, I can see that the listdir check the file in the correct order not the sorted one. However I didnt not success to use "os.listdir(sourdir+"/")[i]" in a loop AND filtering by .txt in the same time.

If someone could help me I really don't know what to do now

  • 3
    Your question depends on the claim that there is a "same order in which they are in the folder!" But is there such an order? I think the order you see the files in depends on a lot of factors (such as LC_COLLATE in unix), but there is practically no _inherent_ order. – kojiro Nov 30 '21 at 15:35
  • 2
    "I just want to read the name of the file in the same order in which they are in the folder ": why, actually? What problem you are trying to solve is so dependent on the order you view the files in your system (which, then, depends on what your viewer does). – 9769953 Nov 30 '21 at 15:36
  • 1
    According to the documentation for `glob.glob`: "Whether or not the results are sorted depends on the file system." Why do you care that they are, or are not, sorted? – jarmod Nov 30 '21 at 15:37
  • Aren't files always sorted anyway? Wether it's by name, size or date...? – Neox Nov 30 '21 at 15:39
  • What are you really trying to achieve? I imagine that your need for a certain order is due to the processing you will perform. – mozway Nov 30 '21 at 15:40
  • As others have mentioned, it's hard to reliably control the order in which the files are given to you. However, it's possible for you to sort the files in an order that satisfies you. Here, your problem appears to be that you want the files to be sorted in order of their numbers, so that 10 comes after 2, rather than in lexicographical order, which makes "10" come before "2". See this question: [Is there a builtin function for string natural sort in python?](https://stackoverflow.com/questions/4836710/is-there-a-built-in-function-for-string-natural-sort) – Stef Nov 30 '21 at 15:58
  • Files names are incremented as they are created. File1 is created, then File2, .... I need to read the files in the order that they are created. Because after I want to plot the result, the x axis being the # of the file. So I need to sort them by name (as the index is in the name) or by date of creation – chang thenoob Nov 30 '21 at 16:47

2 Answers2

1

Explaining my problem helped me to aim at what to do to solve it, so a workaround was to sort by date of creation of files, which I finally managed to do with :

sorted_by_mtime_ascending = sorted(listTdmsFiles, key=lambda t: os.stat(t).st_mtime)
0

I am new to StackOverflow, unable to comment. As Jarmod stated, you either "care" or "don't care" if they are sorted. You seem to care.

So, if you are looking for the most efficient code, this answer will not please you.

>>> import os
>>> import fnmatch
>>> path = '/temp/'
>>> unsorted_list = [file_name for file_name in next(os.walk(path))[2]]
>>> unsorted_filtered_list = fnmatch.filter(unsorted_list,'*.txt')
>>> print(unsorted_list)
['3.txt', '7.yyy', '2.txt', '5.txt', '1.txt', '4.txt', '6.ttt', '1.aaa']
>>> print(unsorted_filtered_list)
['3.txt', '2.txt', '5.txt', '1.txt', '4.txt']
Razzmon
  • 1
  • 2
  • I care because I need to plot results in function of the file number. So I have to sort them either by name (index in the name) or by date of creation. I'm new to python so I didn't really understood your answer, but does that mean there is no way of doing so ? It seems to me as something that should be really easy to do I can't believe it's that much complicated/unfeasible – chang thenoob Nov 30 '21 at 16:50
  • 1
    I was new to Python not so long ago, so I understand. Here is a good reference to how to sort and filter files by name. I hope this helps. Best of Luck ! https://thispointer.com/python-get-list-of-files-in-directory-sorted-by-name/ – Razzmon Nov 30 '21 at 18:46
  • Unfortunately that's already what I'm doing, and I don't want to sort by names. Sorting by date of creation would be nice but I don't success in doing that – chang thenoob Dec 01 '21 at 12:11
  • Have you looked at this stack overflow entry? https://stackoverflow.com/questions/168409/how-do-you-get-a-directory-listing-sorted-by-creation-date-in-python – Razzmon Dec 02 '21 at 14:02