- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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)
Running the above code will give you listing sorted by creation date, for example,
Mon Oct 23 18:01:25 2017 sorted_ls.py
- 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 the creation date of a MySQL table?
- How to get creation and modification date/time of a file using Python?
- How do I get the parent directory in Python?
- How do you create a date object from a date in Swift xcode?
- How do you create a date object from a date in Swift xcode in iOS?
- How do you copy a Lua table by value?
- How to set creation and modification date/time of a file using Python?
- How to get the home directory in Python?
- How do I list all files of a directory in Python?