
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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.
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 Questions & Answers
- 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 get a timestamp in JavaScript?
- How do you convert a JavaScript date to UTC?
- 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?
Advertisements