DECROMAX

Sorting files from newest to oldest (.csv example)

Feb 13th, 2022
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. """Sorts dir from newest to oldest and returns list, access files by index"""
  2.  
  3. import os
  4. import glob
  5.  
  6. files_path = os.path.join('path to folder', '*.csv') # change file extension to suit
  7. files = sorted(glob.iglob(files_path), key=os.path.getctime, reverse=True) # sorts files from newest to oldest
  8.  
  9. newest_file = files[0] # newest .csv file, can use index to access file
  10. second_newest_file = files[1] # example
  11. oldest_file files[-1] example
  12.  
Advertisement
Add Comment
Please, Sign In to add comment