geohernandez
Menu
  • About me
  • Contact me
  • Data Specialist
  • Resume
  • Speaker
  • Speaker Events
Menu

Getting the last modified directory with Python

Posted on November 19, 2022December 18, 2022 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

  • Using interpolated format strings in Python
  • Getting the last modified directory with Python
  • First steps for configuring SQL Server with Docker

Categories

  • Azure (5)
  • Azure DevOps (2)
  • Bash script (1)
  • Blog (1)
  • Cassandra (3)
  • Chronicles from the trenches (23)
  • Data Engineering (5)
  • DB optimization (2)
  • Events (2)
  • GIT (1)
  • MySQL (1)
  • Python (5)
  • SQL Saturday (1)
  • SSIS (2)
  • T-SQL (5)
  • Uncategorized (1)

Archives

  • 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)
© 2023 geohernandez | Powered by Minimalist Blog WordPress Theme