Skip to content Skip to sidebar Skip to footer

Sort Os.listdir Files Python

If have downloaded several years of data stored in files with the following naming convention, year_day.dat. For example, the file named 2014_1.dat has the data for January 1, 2014

Solution 1:

If you don't mind using third party libraries, you can use the natsort library, which was designed for exactly this situation.

importnatsortinList= natsort.natsorted(os.listdir(inDir))

This should take care of all the numerical sorting without having to worry about the details.

You can also use the ns.PATH option to make the sorting algorithm path-aware:

from natsort import natsorted, nsinList= natsorted(os.listdir(inDir), alg=ns.PATH)

Full disclosure, I am the natsort author.

Solution 2:

Try this if all of your files start with '2014_':

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

Otherwise take advantage of tuple comparison, sorting by the year first then the second part of your file name.

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

Solution 3:

dict.items returns a list of (key, item) pair.

the key function is only using the first element (d[0] => key => city).

There's another problem: sorted returns a new copy of the list sorted, and does not sort the list inplace. Also the OrderedDict object is created and not assigned anywhere; Actually, you don't need to sort each time you append the item to the list.

Removing the ... sorted ... line, and replacing following line:

withopen(outFileName, 'w') as f:
     for city, valuesin d.items():
        f.write('{} {}\n'.format(city, ' '.join(values)))

with following will solve your problem:

with open(outFileName, 'w') as f:
     for city, values in d.items():
        values.sort(key=lambda fn: map(int, os.path.splitext(fn)[0].split('_')))
        f.write('{} {}\n'.format(city, ' '.join(values)))

BTW, instead of manually joining hard-coded separator /, use os.path.join:

inDir + "/" + fileName

 =>

os.path.join(inDir, fileName)

Post a Comment for "Sort Os.listdir Files Python"