Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 694563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:52:10+00:00 2026-05-14T02:52:10+00:00

I’m looking for a solution to the following: Given today’s date, figure out what

  • 0

I’m looking for a solution to the following:

Given today’s date, figure out what month was before. So 2 should return for today, since it is currently March, the third month of the year. 12 should return for January.

Then based on that, I need to be able to iterate through a directory and find all files that were created that month.

Bonus points would include finding the most current file created for the previous month.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:52:10+00:00Added an answer on May 14, 2026 at 2:52 am

    Simplest, where adate is an instance of datetime.date:

    def previousmonth(adate):
        m = adate.month - 1
        return m if m else 12
    

    There’s no real way in most Unix filesystems to determine when a file was created, as they just don’t keep that information around. Maybe you want the “latest inode change time” (could be creation, could be some other inode change):

    import os, datetime
    def cmonth(filename):
        ts = os.stat(filename).st_ctime
        return datetime.date.fromtimestamp(ts).month
    

    Of course, this could mean that month in any year — you sure, in both questions, you don’t want the year as well as the month? That would be the .year attribute.

    Anyway, sticking with month only, as per your question, for a single directory (which is the letter of your question), to get all files you can use os.listdir (for a tree rooted in the directory you’d use os.walk instead). Then to keep only those with latest-inode-change in a given 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 could code this as a list comprehension, but there’s just too much code there for a listcomp to be elegant and concise.

    To get the “latest” time, you can either repeat the os.stat call (slower but probably simpler), or change cmonth to return the timestamp as well. Taking the simple route:

    def filetimestamp(fullname):
        return os.stat(fullname).st_ctime
    

    Now, the “most recent file” given a list files of files’ full names (i.e. inc. path) is

    max(files, key=filetimestamp)
    

    Of course there are many degrees of freedom in how you put this all together, depending on your exact specs — given that the specs don’t appear to be necessarily precise or complete I’ve chosen to show the building blocks that you can easily tweak and put together towards your exact needs, rather than a full-blown solution that would likely solve a problem somewhat different from your actual one;-).

    Edit: since the OP clarified that they need both year and month, let’s see what changes would be needed, using tuples ym for (year, month) in lieu 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)
            # if you need to avoid subdirs, uncomment the following line
            # if not os.path.isfile(fullname): continue
            if whatym == cym(fullname):
                results.append(fullname)
        return results
    

    Nothing deep or difficult, as you can see (I also added comments to show how to skip subdirectories if you’re worried about those). And btw, if what you actually need is to walk a subtree, rather than just a directory, that change, too, is pretty 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.