4

I have a list of file:

foo_00.txt
foo_01.txt
foo_02.txt
foo_03.txt
foo_04.txt
foo_05.txt
foo_06.txt
foo_07.txt
foo_08.txt
foo_09.txt
foo_10.txt
foo_11.txt
.........
.........
foo_100.txt
foo_101.txt

when i use

import glob
PATH = "C:\testfoo"
listing = glob.glob(os.path.join(PATH, '*.txt'))

i have this order

    foo_00.txt
    foo_01.txt
    foo_02.txt
    foo_03.txt
    foo_04.txt
    foo_05.txt
    foo_06.txt
    foo_07.txt
    foo_08.txt
    foo_09.txt
    foo_100.txt
    foo_101.txt
    .........
    .........
    foo_10.txt
    foo_11.txt
    .........

i tried also sorted(glob.glob(os.path.join(PATH, '*.txt'))) but without resolve my problem because I wish to have the right sequence. After foo_09.txt i wish to import foo_10.txt and not foo_100.txt and so on.

Gianni Spear
  • 7,033
  • 22
  • 82
  • 131
  • And your problem is ...... ? – Games Brainiac Oct 29 '13 at 12:20
  • sorry if i was not clear. I wish to have the right sequence. After foo_09.txt i wish to import foo_10.txt and not foo_100.txt – Gianni Spear Oct 29 '13 at 12:22
  • You want a natural numeric sort; there are several questions here on SO already that cover that. – Martijn Pieters Oct 29 '13 at 12:22
  • 3
    [Does Python have a built in function for string **natural sort**?](http://stackoverflow.com/questions/4836710/does-python-have-a-built-in-function-for-string-natural-sort) – Ashwini Chaudhary Oct 29 '13 at 12:23
  • What sort order puts `foo_100.txt` before `foo_10.txt`, though? The first string is both longer and has a lexicographically larger character (`0`, ASCII 48 vs `.`, ASCII 46) in the first position where they differ. – chepner Oct 29 '13 at 12:25
  • 1
    This one helped me: https://stackoverflow.com/questions/12093940/reading-files-in-a-particular-order-in-python – ashley Oct 31 '17 at 18:53

1 Answers1

18

You can use a special key function for your sort.

sorted(files, key=lambda name: int(name[4:-4]))

What this does is, it takes the filename, e.g. foo_100.txt, strips away the first 4 and last 4 characters, converts the rest to an int, and sorts by those values.

Of course, this only works if all the files have the same prefix and extension, and you may have to use different numbers for other file names. Alternatively, you can use the split method of string or a regular expression to extract the numeric part in the key function.

tobias_k
  • 81,265
  • 12
  • 120
  • 179