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.

2
  • 4
    "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? Commented Sep 28, 2015 at 12:34
  • Related to Nonalphanumeric list order from os.listdir() in Python Commented Sep 28, 2015 at 12:40

3 Answers 3

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))
Sign up to request clarification or add additional context in comments.

5 Comments

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")
No it is not working as Kevin said. I am getting something like this-539.txt 54.txt 540.txt 541.txt
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 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
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.
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']

Comments

0

you can try sorting them by using natsort:

import natsort
unsorted_files = ['2.txt','3.txt','1.txt']
sorted_files = natsort.natsorted(unsorted_files)
print(sorted_files)

you need to install the package if not installed by using :

pip install natsort

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.