0
for root, dirs, files in 
os.walk(r'\\cusfs01\userprof$\khalha\Desktop\AllSalesForecasting'):
    for name in files:
        sorted(files, key=lambda)
        print(files)

I have found many examples of sorting with physical lists, but since my list of files is just in this directory, I do not know how to sort it correctly. I have 54 files and it is doing that thing where 9 appears to be the latest file where I want 54 to appear as the last. it may seem like a duplicate question but I don't know how to fix this issue and the other questions similiar do not address this. My files are all named number starting from 0 to 54. They are csv filesm so the files are 0.csv,1.csv,...54.csv. When i print the files, or try to get the last file name, it shows it as 9.csv. I want them to be listed numerically

jpp
  • 159,742
  • 34
  • 281
  • 339
Michael Norman
  • 121
  • 2
  • 5
  • 13
  • 1
    What other questions? Show your research. And what, exactly, do you mean by "sorting". Sort by what criteria, exactly? –  Jul 12 '18 at 16:35
  • 1
    also, `sorted` does not do an in-place sort. It returns a new list. you might want to do: `files = sorted(files)` and then `print(files)` – Sreejith Menon Jul 12 '18 at 16:39
  • @jdv I apologize, I editted my question to show what my files look like – Michael Norman Jul 12 '18 at 16:46
  • @jpp I have edited my question – Michael Norman Jul 12 '18 at 16:46
  • Related: https://stackoverflow.com/questions/3426108/how-to-sort-a-list-numerically Can you extract the numbers from the filenames? – VPfB Jul 12 '18 at 16:47
  • @VPfB Those type of questions seem to only help when you physically write out the list. And yes when I print the file names, they print 0,1,10,11,...,19,20,21...29,30...50,51,..54,9 in that type of order – Michael Norman Jul 12 '18 at 16:50
  • I think you may be confusing numeric sorting with alphabetic sorting? –  Jul 12 '18 at 16:57
  • @jdv i apologize im new to coding in general, I want to order the files from smallest to largest in number, I am unsure what the word for that is. – Michael Norman Jul 12 '18 at 17:00
  • "54" comes before "9" in the "collation sequence" when those are treated as strings or characters. 54 comes after 9 numerically. I think this answer is where you should concentrate: https://stackoverflow.com/a/51310829/1531971 –  Jul 12 '18 at 17:52

4 Answers4

1
>>> import os
>>> path = r'\\cusfs01\userprof$\khalha\Desktop\AllSalesForecasting'
>>> sorted(sum([files for _,_,files in os.walk(path)], []), key=lambda f: int(f.split('.')[0]))
['1.csv', '2.csv', '3.csv', '4.csv', '5.csv', '6.csv', '7.csv', '8.csv', '9.csv', '10.csv', '11.csv', '12.csv', '13.csv', '14.csv', '15.csv', '16.csv', '17.csv', '18.csv', '19.csv', '20.csv', '21.csv', '22.csv', '23.csv', '24.csv', '25.csv', '26.csv', '27.csv', '28.csv', '29.csv', '30.csv', '31.csv', '32.csv', '33.csv', '34.csv', '35.csv', '36.csv', '37.csv', '38.csv', '39.csv', '40.csv', '41.csv', '42.csv', '43.csv', '44.csv', '45.csv', '46.csv', '47.csv', '48.csv', '49.csv', '50.csv', '51.csv', '52.csv', '53.csv', '54.csv']
>>> 
Sunitha
  • 11,777
  • 2
  • 20
  • 23
0

If I understand your question, the files are being sorted opposite of the order you'd like.

You might want to look at the docs for sorted()

I suggest using the reverse flag:

files = sorted(files, key=lambda, reverse=True)

Dan O'Boyle
  • 3,676
  • 5
  • 28
  • 44
0

sorted will sort string alphabetically, and integers and floats numerically. Use the key to give the function a way to sort on the integer values you give the files. Like this:

sorted(files, lambda n: int(n.split('.')[0]))

split to get rid of the file extension and int to convert to integers.

Edit:
Just so you know. sorted returns the sorted list. files.sort will sort in place.

Hielke Walinga
  • 2,677
  • 1
  • 17
  • 30
0

You need to create a list from your iterator and then use sorted with a custom key. Since sorted does not operate in place, you should assign the result to a variable:

path  r'\\cusfs01\userprof$\khalha\Desktop\AllSalesForecasting'

file_list = []
for root, dirs, files in os.walk(path):
    file_list.append(files)

res = sorted(file_list, key=lambda x: int(x.split('.')[0]))

A more Pythonic approach would be to feed an iterable to sorted:

files = (files for root, dir, files in os.walk(path))
res = sorted(files, key=lambda x: int(x.split('.')[0]))

This will be more efficient since internally sorted creates a new list, even if the input is already a list.

jpp
  • 159,742
  • 34
  • 281
  • 339
  • Yes I have tried this method but always seem to get several errors, I tried right now and it says: AtrivbuteError: "list" object has no attrictute "split" – Michael Norman Jul 12 '18 at 16:54