
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How do you get a directory listing sorted by creation date in Python?
To get a directory listing sorted by creation date in Python, you can call os.listdir() to get a list of the filenames. Then call os.stat() for each one to get the creation time and finally sort against the creation time.
example
import os import time import sys from stat import S_ISREG, ST_CTIME, ST_MODE dir_path = '.' # get all entries in the directory entries = (os.path.join(dir_path, file_name) for file_name in os.listdir(dir_path)) # Get their stats entries = ((os.stat(path), path) for path in entries) # leave only regular files, insert creation date entries = ((stat[ST_CTIME], path) for stat, path in entries if S_ISREG(stat[ST_MODE])) print(entries)
Output
Running the above code will give you listing sorted by creation date, for example,
Mon Oct 23 18:01:25 2017 sorted_ls.py
- Related Articles
- How do you get a directory listing sorted by their name in Python?
- MySQL query to display databases sorted by creation date?
- How do I get the creation date of a MySQL table?
- How to get file creation & modification date/times in Python?
- How to get creation and modification date/time of a file using Python?
- How to get the creation date of a MySQL table?
- How do I get the parent directory in Python?
- How do you create a date object from a date in Swift xcode?
- How to set creation and modification date/time of a file using Python?
- How do you get a timestamp in JavaScript?
- How do you create a date object from a date in Swift xcode in iOS?
- How do you convert a JavaScript date to UTC?
- How to get the home directory in Python?
- How do I get an ISO 8601 date in string format in Python?
- How do I list all files of a directory in Python?

Advertisements