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?
You can use the builtin
sorted
function to sort the strings however you want. Based on what you describe,Alternatively, you can use the
.sort
method of a list:I think should do the trick.
Note that the order that
os.listdir
gets the filenames is probably completely dependent on your filesystem.As In case of mine requirement I have the case like
row_163.pkl
hereos.path.splitext('row_163.pkl')
will break it into('row_163', '.pkl')
so need to split it based on '_' also.but in case of your requirement you can do something like
where
and also for directory retrieving you can do
sorted(os.listdir(path))
and for the case of like
'run01.txt'
or'run01.csv'
you can do like thisPer the documentation:
Order cannot be relied upon and is an artifact of the filesystem.
To sort the result, use
sorted(os.listdir(path))
.I found "sort" does not always do what I expected. eg, I have a directory as below, and the "sort" give me a very strange result:
It seems it compares the first character first, if that is the biggest, it would be the last one.