
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do you get a directory listing sorted by their name in Python?
You can call the os.listdir function to get the list of the directory contents and use the sorted function to sort this list.
For example
>>> import os >>> list_dir = os.listdir('.') >>> list_dir = [f.lower() for f in list_dir] # Convert to lower case >>> sorted(list_dir) ['dlls', 'doc', 'etc', 'include', 'lib', 'libs', 'license.txt', 'news.txt', 'python.exe', 'pythonw.exe', 'readme.txt', 'scripts', 'share', 'tcl', 'tools', 'w9xpopen.exe']
- Related Articles
- How do you get a directory listing sorted by creation date in Python?
- How do I get the parent directory in Python?
- How do you get a timestamp in JavaScript?
- How to create a unique directory name using Python?
- How to get the home directory in Python?
- How do I list all files of a directory in Python?
- How to get all the files, sub files and their size inside a directory in C#?
- Java Program to get name of parent directory
- How do you get the current figure number in Python's Matplotlib?
- MySQL random rows sorted by a specific column name?
- What do you mean by a Framework? Name the types of framework available.
- C# Program to get the name of root directory
- How do you code a vending machine in Python?
- How do you Loop Through a Dictionary in Python?
- How do you get the file size in C#?

Advertisements