0

I'm very new to python and can't figure out how to get the .sort() function to work.

I have a folder of .txt files on my computer that I am trying to order by file name. They are titled poem_1.txt through poem_600.txt. so far, this is what I have:

   import os
    all_poems=[]
    for root, dirs, files in os.walk(mydir):
        for file in files:
            if file.endswith(".txt"):
                all_poems.append(os.path.join(root, file))
    n_files = len(all_poems)

and then i can verify that all 600 files have been looped and appended by using

all_poems[600]

then, i do

 all_poems.sort()

and it gives me the poems in this order:

'poem_1.txt', 'poem_10.txt', 'poem_100.txt', 'poem_11.txt', 'poem_12.txt', 'poem_13.txt', 'poem_14.txt', 'poem_15.txt', 'poem_16.txt', 'poem_17.txt', 'poem_18.txt', 'poem_19.txt', 'poem_2.txt', 'poem_20.txt', 'poem_21.txt'

what am I doing wrong?

jane
  • 9
  • 1

1 Answers1

2

Use sort with key to fix:

all_poems = sorted(all_poems, key=lambda x: int(x.split('_')[1].split('.')[0]))

or:

# sort on self.
all_poems.sort(key=lambda x: int(x.split('_')[1].split('.')[0]))

This will sort based on file numbers, results:

>>> all_poems:

['poem_1.txt',
 'poem_2.txt',
 'poem_10.txt',
 'poem_11.txt',
 'poem_12.txt',
 'poem_13.txt',
 'poem_14.txt',
 'poem_15.txt',
 'poem_16.txt',
 'poem_17.txt',
 'poem_18.txt',
 'poem_19.txt',
 'poem_20.txt',
 'poem_21.txt',
 'poem_100.txt']
Rocky Li
  • 5,641
  • 2
  • 17
  • 33