geohernandez
Menu
  • HOME
  • ABOUT
  • CONTACT ME
  • WORK WITH GEO
    • Data Specialist
    • Speaker Events
    • Resume
  • English
    • English
    • EspaƱol
Menu

Getting the last modified directory with Python

Posted on November 19, 2022August 16, 2023 by geohernandez

Working with os paths is a prevalent task, especially when working on a console application in Python. Recently, I had to add a change to an existing feature where it was mandatory to return the last modified directory on a specific target. In Python, we have the opportunity to count with different libraries; in this post, we are going to use glob.

As usual, Python docs is the best place to find a clear and updated definition of each official library; here is the link for glob. The clearest and simple definition of this library is stated in the Python docs and tells us: “The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order”.

The premise is simple, we will receive a pathname and we have to return the last directory created, so the time creation is the filter criteria. Based on this, we will combine the glob function with another function called max.

The following code shows you the syntax of the glob function, which result in a list of matches based on the pattern submitted:

1
2
3
import glob
 
glob.glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False)

Solving my use case requires keeping the recursive parameter as False because I am not interested in diving through subdirectories. However, we still need to cope with a way of getting the last directory created; as I mentioned above, we have to combine it with the max function, more details here.

1
max(iterable, *, key=None)

In our case, the iterable object is the list of pathnames returned as output by the glob function. Nonetheless, we must first check if it is a directory (files have to be excluded). Keep in mind the previous restriction, we could have a great opportunity to make a pythonic code through the use of List Comprehension (liscomps). A list comprehensions make it easy to create lists while performing sophisticated filtering, mapping, and conditional logic on their members. The conditional logic in our liscomps will filter only directories.

Finally, The key argument specifies a one-argument ordering function, we are going to use getmtime which returns the time of the last modification of a path. The list of pathnames is submitted to the key function; it allows the max function makes the modification time its filter criteria.

Note: If you use key=os.path.getsize instead of getmtime, the filter criteria for the max function will be the path size.

The following code joins the functions previously described.

comprenhension list
1
2
3
mypath ="My_path_here"
last_directory= max([dir for dir in glob.glob(mypath + r'\*', recursive=False) if os.path.isdir(dir)],
            key=os.path.getmtime))

I hope this approach helps you to solve similar requirements, keeping your code simple, readable and concise. Happy coding!

Category: Chronicles from the trenches, Data Engineering, Python

Leave a Reply Cancel reply

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

Search for articles

Recent Posts

  • Quick Guide: BigQuery Service Account Setup Using gcloud
  • The Art of Data Modeling in AI times
  • Getting Started with Snowflake’s Snowpipe for Data Ingestion on Azure

Categories

  • Airflow (1)
  • Azure (6)
  • Azure DevOps (2)
  • Bash script (1)
  • Blog (1)
  • Cassandra (3)
  • Chronicles from the trenches (26)
  • Data Architecture (3)
  • Data Engineering (11)
  • DB optimization (2)
  • Events (2)
  • GIT (1)
  • MySQL (1)
  • Python (7)
  • Snowflake (3)
  • SQL Saturday (1)
  • SSIS (2)
  • T-SQL (5)
  • Uncategorized (2)

Archives

  • May 2025 (1)
  • March 2025 (1)
  • January 2025 (2)
  • October 2024 (1)
  • July 2024 (1)
  • May 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • August 2023 (1)
  • June 2023 (1)
  • December 2022 (1)
  • November 2022 (1)
  • July 2022 (1)
  • March 2022 (1)
  • September 2021 (1)
  • May 2021 (1)
  • March 2021 (1)
  • February 2021 (3)
  • December 2020 (1)
  • October 2020 (3)
  • September 2020 (1)
  • August 2020 (1)
  • January 2020 (1)
  • August 2019 (1)
  • July 2019 (1)
  • June 2019 (1)
  • May 2019 (1)
  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (3)
  • October 2018 (1)
  • September 2018 (1)
  • August 2018 (2)
© 2025 geohernandez | Powered by Minimalist Blog WordPress Theme