The simplest is where adate is an instance of datetime.date :
def previousmonth(adate): m = adate.month - 1 return m if m else 12
On most Unix file systems, there is no real way to determine when a file was created, because they simply do not support this information. Perhaps you need a "last time changing the inode" (there may be a creation, there may be another inode change):
import os, datetime def cmonth(filename): ts = os.stat(filename).st_ctime return datetime.date.fromtimestamp(ts).month
Of course, this can mean this month in any year - are you sure that in both issues you do not need a year , as well as a month? This will be the .year attribute.
In any case, sticking to only a month, according to your question, for one directory (which is the letter of your question) to get all the files that you can use os.listdir (for a tree embedded in the directory that you use os.walk ). Then, to save only those with the last-inode-change for a specific month:
def fileswithcmonth(dirname, whatmonth): results = [] for f in os.listdir(dirname): fullname = os.path.join(dirname, f) if whatmonth == cmonth(fullname): results.append(fullname) return results
You can code this as a list comprehension, but there is too much code to make listcomp elegant and concise.
To get the βlastβ time, you can either repeat the os.stat call (slower, but probably easier), or change cmonth to return the timestamp. Taking a simple route:
def filetimestamp(fullname): return os.stat(fullname).st_ctime
Now the "most recent file" specified in the files list of full file names (i.e., inc. Path),
max(files, key=filetimestamp)
Of course, there are many degrees of freedom in how you combine all this depending on your specific characteristics - given that the specifications do not necessarily seem accurate or complete. I decided to show building blocks that you can easily customize and combine your specific needs, rather than a full-blown solution that is likely to solve a problem that is slightly different from your actual one; -).
Edit : since the OP clarified that they need a year and a month, let's see what changes will be needed using the ym tuples for (year, month) instead of the bare month:
def previousym(adate): y = adate.year m = adate.month - 1 return (y, m) if m else (y - 1, 12) import os, datetime def cym(filename): ts = os.stat(filename).st_ctime dt datetime.date.fromtimestamp(ts) return cym.year, cym.month def fileswithcym(dirname, whatym): results = [] for f in os.listdir(dirname): fullname = os.path.join(dirname, f)
Nothing serious or complicated, as you can see (I also added comments to show how to skip subdirectories if you are worried about this). And btw, if you really need to walk in a subtree, and not just with a directory, this change is also quite localized:
def fileswithcymintree(treeroot_dirname, whatym): results = [] for dp, dirs, files in os.walk(treeroot_dirname): for f in files: fullname = os.path.join(dp, f) if whatym == cym(fullname): results.append(fullname) return results