Sometimes, we want to change list order from os.listdir() with Python.
In this article, we’ll look at how to change list order from os.listdir() with Python.
How to change list order from os.listdir() with Python?
To change list order from os.listdir() with Python, we can call sorted
on the iterator returned by listdir
.
For instance, we write
l = sorted(os.listdir(whatever_directory))
to call sorted
on the iterator that’s returned by os.listdir
.
We call os.listdir
with the root directory path to get the items inside it.
Conclusion
To change list order from os.listdir() with Python, we can call sorted
on the iterator returned by listdir
.