Python method listdir() returns a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.
os. listdir() method in python is used to get the list of all files and directories in the specified directory. If we don't specify any directory, then list of files and directories in the current working directory will be returned.
You can call the os. listdir function to get the list of the directory contents and use the sorted function to sort this list.
Yes, it is deterministic, it's certainly not purposefully randomised.
You can use the builtin sorted
function to sort the strings however you want. Based on what you describe,
sorted(os.listdir(whatever_directory))
Alternatively, you can use the .sort
method of a list:
lst = os.listdir(whatever_directory)
lst.sort()
I think should do the trick.
Note that the order that os.listdir
gets the filenames is probably completely dependent on your filesystem.
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.
Per the documentation:
os.listdir(path)
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
Order cannot be relied upon and is an artifact of the filesystem.
To sort the result, use sorted(os.listdir(path))
.
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:
import re
def sorted_alphanumeric(data):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(data, key=alphanum_key)
You can now use this function to sort a list:
dirlist = sorted_alphanumeric(os.listdir(...))
PROBLEMS:
In case you use the above function to sort strings (for example folder names) and want them sorted like Windows Explorer does, it will not work properly in some edge cases.
This sorting function will return incorrect results on Windows, if you have folder names with certain 'special' characters in them. For example this function will sort 1, !1, !a, a
, whereas Windows Explorer would sort !1, 1, !a, a
.
So if you want to sort exactly like Windows Explorer does in Python you have to use the Windows built-in function StrCmpLogicalW via ctypes (this of course won't work on Unix):
from ctypes import wintypes, windll
from functools import cmp_to_key
def winsort(data):
_StrCmpLogicalW = windll.Shlwapi.StrCmpLogicalW
_StrCmpLogicalW.argtypes = [wintypes.LPWSTR, wintypes.LPWSTR]
_StrCmpLogicalW.restype = wintypes.INT
cmp_fnc = lambda psz1, psz2: _StrCmpLogicalW(psz1, psz2)
return sorted(data, key=cmp_to_key(cmp_fnc))
This function is slightly slower than sorted_alphanumeric()
.
Bonus: winsort
can also sort full paths on Windows.
Alternatively, especially if you use Unix, you can use the natsort
library (pip install natsort
) to sort by full paths in a correct way (meaning subfolders at the correct position).
You can use it like this to sort full paths:
from natsort import natsorted, ns
dirlist = natsorted(dirlist, alg=ns.PATH | ns.IGNORECASE)
Starting with version 7.1.0 natsort supports os_sorted
which internally uses either the beforementioned Windows API or Linux sorting and should be used instead of natsorted()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With