Gentlemen, is there a quick and convenient way to go through the file folder in order of the files in it? os.walk, os.listdir, nothing helped me, I ran in different folders, but the result is the same - the files in the generated list are issued in random order. In the folder there are 26 files with the names _img_order number.png

code:

path = r"C:\Users\neir0\Desktop\studyV1\data\clean" print(sorted(os.listdir(path))) 

output: files are mixed

['_img_1.png', '_img_10.png', '_img_11.png', '_img_12.png', '_img_13.png', '_img_14.png', '_img_15.png', '_img_16.png', ' _img_17.png ',' _img_18.png ',' _img_19.png ',' _img_2.png ',' _img_20.png ',' _img_21.png ',' _img_22.png ',' _img_23.png ',' _img_24. png ',' _img_25.png ',' _img_26.png ',' _img_3.png ',' _img_4.png ',' _img_5.png ',' _img_6.png ',' _img_7.png ',' _img_8.png ' , '_img_9.png']

pending: list of files according to their location in the folder

  • 2
    And what is the order of the files in the folder ? - mkkik 2:41 pm
  • What does "in order of their location in it" mean? - nick_gabpe 2:41 pm
  • let's say the files in the folder are 1.txt, 2.txt, 3.txt ..., they are ordered by name. When using os.walk, they change places - Sahar Vkusni
  • They are ordered by name only when you want it (setting a file manager, for example). And in your case, sorted(os.listdir(PATHTODIR)) can help. - mkkik
  • sorted will not help me if the file name is not a digit. I tried to use it before - Sahar Vkusni

2 answers 2

If you do not bind to the structure in the file name, you can do this:

 print(sorted(items, key=lambda x: int(''.join(filter(str.isdigit, x))))) 

To make the code look more decent, add a function:

 def get_num(text): return int(''.join(filter(str.isdigit, text))) print(sorted(items, key=get_num)) 

Sort the files as numbers:

 items = os.listdir(path) print(sorted(items, key=lambda x: int(x.split('_')[2].split('.')[0]))) 

This code int(x.split('_')[2].split('.')[0]) pulls out the number from the file name and leads it to int :

  • _img_1.png -> 1
  • _img_21.png -> 21

Similarly, you can do through the regular season:

 import re def get_num(text): match = re.search('_img_(\d+).png', text) if match: return int(match[1]) print(sorted(items, key=get_num)) 
  • Thank you, of course, but I wrote this before. Just want to sort out out of the box - Sahar Vkusni
  • one
    @SaharVkusni, and the third option? - gil9red
  • although not out of the box, but four ways and painted - Sahar Vkusni
  • @SaharVkusni, but the first one (he’s the last one) is best suited out of the box :) - gil9red
  • I agree, however, due to the additional library, the question is quite controversial. She's not in the box - Sahar Vkusni

The order is not at all random - it is the usual lexicographical sorting. And you want, apparently, the so-called Alphanumeric sorting, which takes into account the numbers included in the string exactly in numerical order. On Windows, there is a string comparison function StrCmpLogicalW , in GNU sort the option -V, --version-sort natural sort of (version) numbers within text

I think it is not difficult to find implementations of such sorting (alphanumeric or sometimes natural sort) in Python - you just have to make sure that if there are several numbers, everything continues to work.

Here is the natsort library. Example from the link below

 >>> import natsort >>> your_list = set(['booklet', '4 sheets', '48 sheets', '12 sheets']) >>> print ',\n'.join(natsort.natsorted(your_list)) 4 sheets, 12 sheets, 48 sheets, booklet 

Here are some other implementations (maybe some of them allocate only one number)

  • Add an example of working with natsort - gil9red