Search code examples
pythonpython-3.xtext-search

How to extract the current month from this string?


In the given code below I want to get the current month (October if I run it today) instead of "current_month" in monthpage which is "file/monthly/current_month".

How can it be done?

def CurrentMonth(self):  
    monthpage = "file/monthly/current_month"
    page = Page
    old_text = page.get(get_redirect=True)

Solution

  • from datetime import datetime
    now = datetime.now()
    monthpage.replace('current_month',now.strftime("%B"))
    

    It replaces the "current_month" string in monthpage variable with actual current month name