• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Sort Files by Date in Python

Sort Files by Date in Python

May 16, 2022 Leave a Comment

To sort files by date using Python, you can use the os module listdir() function to get all files in a directory. Then use the
os.path.getcttime() or os.path.getmttime() to get the file creation or modification dates, respectively, inside a sort function.

import os

files = os.listdir()

print(files)

files.sort(key=lambda x: os.path.getmtime(x))  #Sort by Modification Time

print(files)

#Output
['code1.txt', 'code2.txt', 'code3.txt']
['code1.txt', 'code3.txt', 'code2.txt']

When working with files in Python, the ability to get a list of files in a directory and sort by date can be useful.

With the help of the Python os module, we can easily get a list of files and sort them by either creation date or modification date.

The Python os module has many great functions which help us interact with the operating system of our computer.

Let’s say we have the following directory with three files.

sort files by date python

To get the files in this directory, we first use the os module listdir() function. Then, we have two options for sorting; we can sort by file modification date or by file creation date.

How to Sort Files by File Modification Date with Python

If you want to sort files by file modification date, the easiest way is to sort using a lambda function and use the os.path.os.path.getmtime() function.

Below shows you how you would sort files by modification date in Python for our example directory.

import os

files = os.listdir()

print(files)

files.sort(key=lambda x: os.path.getmtime(x))  #Sort by Modification Time

print(files)

#Output
['code1.txt', 'code2.txt', 'code3.txt']
['code1.txt', 'code3.txt', 'code2.txt']

How to Sort Files by File Creation Date with Python

If you want to sort files by file creation date, the easiest way is to sort using a lambda function and use the os.path.os.path.getctime() function.

Below shows you how you would sort files by creation date in Python for our example directory.

import os

files = os.listdir()

print(files)

files.sort(key=lambda x: os.path.getctime(x))  #Sort by Creation Time

print(files)

#Output
['code1.txt', 'code2.txt', 'code3.txt']
['code1.txt', 'code2.txt', 'code3.txt']

Hopefully this article has been useful for you to learn how to sort files by date in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Reverse Tuple
  • 2.  Using Python to Append Character to String Variable
  • 3.  Convert pandas Series to Dictionary in Python
  • 4.  Using Python to Return Two Values from Function
  • 5.  Python Check if Object is Iterable with hasattr() Function
  • 6.  Drop Last Row of pandas DataFrame
  • 7.  Print Approximately Equal Symbol in Python
  • 8.  pandas head – Return First n Rows from DataFrame
  • 9.  Convert String to Boolean Value in Python
  • 10.  How to Filter Lists in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy