Getting the last modified directory with Python

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:

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.

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.

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