python-3.xdatetimedirectorysubprocessends-with

Python to get path of folder that endswith today's date


There are a lot of subdirectories in a directory. Every Subdirectory name ends with a date. I want to to open a subdirectory that ends with today's Date.

Here is my code so far. But it is not Working.

import datetime
now = datetime.datetime.now()
currentdate = now.strftime("%d")
currentmonth = now.strftime("%B")
print(currentdate)
print(currentmonth)


path = 'C:\\Users\\Mondoc\\Desktop\\SweetMemories'

for folder in path:
    if folder.endswith(currentdate):
        pathtofolder = folder
import subprocess
subprocess.Popen('C:\\Users\\Mondoc\\Desktop\\SweetMemories'+pathtofolder)

My code is not working


Solution

  • You are iterating a string actually. Is this what you want?

    import os
    
    # for folder in path:
    #    if folder.endswith(currentdate):
    for name in os.listdir(path):
        if os.path.isdir(name) and name.endswith(...):
            ...