0

I am trying to read all files within a directory.

for root, dirs,files in os.walk(path):
        for j in files:
            print(str(j))

This is my code where path is the path of the directory to be read.But it does not print the files in the order of their names. In my case, I have files from 0.txt,1.txt,.... to 3590.txt. I want it to print the files in the same order. But rather it starts from 579.txt . How can I fix this? I want to do some computation on the files in the same order and so just storing the names in a list and sorting it wont help.

Noober
  • 1,516
  • 4
  • 22
  • 48
  • 3
    _"I want to do some computation on the files in the same order and so just storing the names in a list and sorting it wont help."_ You can use a list and still perform computations on its contents. Can you explain further why this wouldn't work? – Kevin Sep 28 '15 at 12:34
  • Related to [Nonalphanumeric list order from os.listdir() in Python](http://stackoverflow.com/questions/4813061/nonalphanumeric-list-order-from-os-listdir-in-python) – Rodrigue Sep 28 '15 at 12:40

2 Answers2

4

what about sorting them with a lambda to use the int in the filename:

for root, dirs,files in os.walk(path):
    for j in sorted(files, key=lambda key: int(key.replace(".txt", ""))):
        print(str(j))
RickyA
  • 15,465
  • 5
  • 71
  • 95
  • 1
    This won't sort them numerically, though. For example, `sorted(["2.txt", "11.txt", "300.txt"])` gives `['11.txt', '2.txt', '300.txt']`. (this may actually be what the OP means by "sorting won't help") – Kevin Sep 28 '15 at 12:40
  • No it is not working as Kevin said. I am getting something like this-539.txt 54.txt 540.txt 541.txt – Noober Sep 28 '15 at 12:40
  • If lexicographic sorting was really the problem though, I wouldn't expect to see "539.txt" first, since "1.txt" and "0.txt" still precede it alphabetically. – Kevin Sep 28 '15 at 12:42
  • @Kevin Yes `0.txt` and `1.txt` appear before `539` . I just posted three files where it is failing.In actual output,`0` and `1` appear before `539` – Noober Sep 28 '15 at 12:45
  • If you want the sorting to be in numerical order rather than lexicographic, you could use the natsort library. `from natsort import natsorted` then replace `sorted` in the above code with `natsorted`. – J. Corson Sep 28 '15 at 13:55
0

As file name structure is like $Number$.txt, so we can sort by Number.

Demo:

>>> l = ["0.txt", "1.txt", "33.txt", "2.txt", "10.txt", "11.txt"]
>>> sorted([(int(i.replace(".txt", "")), i) for i in l ])
[(0, '0.txt'), (1, '1.txt'), (2, '2.txt'), (10, '10.txt'), (11, '11.txt'), (33, '33.txt')]
>>> [i[1] for i in sorted([(int(i.replace(".txt", "")), i) for i in l ])]
['0.txt', '1.txt', '2.txt', '10.txt', '11.txt', '33.txt']
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56