Categories
Python Answers

How to change list order from os.listdir() with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *