Skip to main content

Posts

Showing posts from April, 2010

Python Magic – Find the Most Recently Modified File

I had a very interesting assignment today. Find the most recently modified file in the CVS repository!   Write a program in Java? This is my first choice if any problem comes to my mind! Well, iterating through tens of thousands of file to find the latest modified file. This can be a killer program if do it in java! Over the years I kind of looked down at the Scripting languages. I was amazed- how few lines of Python code can do wonders!   import os, time, glob   folder = "<FOLDER NAME IN UPPER CASE>" oldModifiedFileName = "" oldModifiedFileDate = "" lastmod_date = ""   for infile in glob.glob( os.path.join(folder, '*') ):     for (path, dirs, files) in os.walk(folder):         for file in files:             filename = os.path.join(path, file)             stats = os.stat(filename)             lastmod_date = time.localtime(stats[8])             file_date = time.strftime("%Y%m%d %H:%M:%S", lastmod_date)               if file