I often use python to process directories of data. Recently, I have noticed that the default order of the lists has changed to something almost nonsensical. For example, if I am in a current directory containing the following subdirectories: run01, run02, ... run19, run20, and then I generate a list from the following command:
dir = os.listdir(os.getcwd())
then I usually get a list in this order:
dir = ['run01', 'run18', 'run14', 'run13', 'run12', 'run11', 'run08', ... ]
and so on. The order used to be alphanumeric. But this new order has remained with me for a while now.
What is determining the (displayed) order of these lists?
Python for whatever reason does not come with a built-in way to have natural sorting (meaning 1, 2, 10 instead of 1, 10, 2), so you have to write it yourself:
You can now use this function to sort a list:
The proposed combination of os.listdir and sorted commands generates the same result as ls -l command under Linux. The following example verifies this assumption:
So, for someone who wants to reproduce the result of the well-known ls -l command in his Python code, sorted( os.listdir( DIR ) ) works pretty well.
I think the order has to do with the way the files are indexed on your FileSystem. If you really want to make it adhere to some order you can always sort the list after getting the files.
Elliot's answer solves it perfectly but because it is a comment, it goes unnoticed so with the aim of helping someone, I am reiteration it as a solution.
Use natsort library:
Install the library with the following command for Ubuntu and other Debian versions
Python 2
Python 3
Details of how to use this library is found here
It's probably just the order that C's
readdir()
returns. Try running this C program:The build line should be something like
gcc -o foo foo.c
.P.S. Just ran this and your Python code, and they both gave me sorted output, so I can't reproduce what you're seeing.