w3resource
Python Exercises

Python: Sort files by date

Python Basic: Exercise-70 with Solution

Write a Python program to sort files by date.

Sample Solution:-

Python Code:

import glob
import os

files = glob.glob("*.txt")
files.sort(key=os.path.getmtime)
print("\n".join(files))

Sample Output:

result.txt                                                                                                    
temp.txt                                                                                                      
myfile.txt                                                                                                    
mynewtest.txt                                                                                                 
mytest.txt                                                                                                    
abc.txt                                                                                                       
test.txt 

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to sort three integers without using conditional statements and loops.
Next: Write a Python program to get a directory listing, sorted by creation date.

What is the difficulty level of this exercise?