Shan Sundar
Shan Sundar

Reputation: 37

Reading files from from the folder as the same order in which they appears

I have one folder. In that I have some files in the order, filename_100, filename_101, filename_200, filename_201. I have to read the files in the same order. I have used the below code. But I can't get the expected output. Randomly it is reading files from the folder.

Code:

for subdir, dirs, files in os.walk(order_path):
   for file in files:
        filepath = subdir + os.sep + file

Can someone suggest me better solution for this?

Upvotes: 2

Views: 1982

Answers (2)

jbndlr
jbndlr

Reputation: 5210

I doubt that files have any order in a directory. But if you want to sort them, just use sorted(...):

for subdir, dirs, files in os.walk(order_path):
    for f in sorted(files):
        print(os.path.join(subdir, f))

Edited: Better use os.path.join(...) to join the file to the path, it takes care of your os's separators, etc.

If you want to sort by any number following the last _ in your filename, you can change the sorting-directive to something like:

sorted(files, key=lambda x: x.split('_')[-1])

Upvotes: 1

Martin Evans
Martin Evans

Reputation: 46759

You might want to consider sorting your filenames numerically as follows:

def get_number(filename):
    if filename.find('_') != -1:
        name, number = os.path.splitext(filename)[0].split('_')
        return (name, int(number))
    else:
        return filename

for subdir, dirs, files in os.walk(order_path):
    for file in sorted(files, key=get_number):
        filepath = os.path.join(subdir, file)
        print filepath

This would then cope with the following kind of naming:

filename_1.txt
filename_100.txt
filename_101.txt
filename_200.txt
filename2_1.txt
filename2_100.txt
filename2_101.txt
filename2_200.txt

The function spots any file containing an _ and then converts the number into an integer, it returns a tuple holding the name so that differently named files are grouped together.

If you are more interested in just the numbers, i.e. each of the files has a different name, but you still want to keep the numerical order, then replace the following line in the get_number() function:

return (int(number), name)

Upvotes: 1

Related Questions