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:

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.

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.

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!

geohernandez

Share
Published by
geohernandez
Tags: getmtime

Recent Posts

Formatting our Postgres scripts with pgformatter in DBeaver

Are you a PostgreSQL enthusiast using DBeaver on a Windows Platform? If you find yourself…

4 months ago

Looking back to Kimball’s approach to Data Warehousing

Over time, it's fascinating to witness how certain concepts, approaches, or visions age. Time, in…

5 months ago

List Comprehension and Walrus operator in Python

When we are working with lists, dictionaries, and sets in Python, we have a special…

9 months ago

Playing with some Pandas functions and Airflow operators

Recently, I was dealing with a task where I had to import raw information into…

11 months ago

Using interpolated format strings in Python

The release of Python 3.6 came with exciting functionalities. I want to speak about the…

1 year ago

First steps for configuring SQL Server with Docker

Sometimes, our working habits could experiment some interesting changes, maybe forced by real needs or…

2 years ago