How do I pick files with respect to a particular folder in python?

I have a specific folder structure laid down by the business which is "/content/2020/May/13-05-2020". At present I am consuming all files from this location.

But what I would rather want is to pick/consume files based on a daily batch process(as date being mentioned in file path).

To make it simple let us say that in the file path if today's date, May month and 2020 year present then it should process the file using "/content/2020/May/13-05-2020" .

Else it should check for the year , month and date in the same way and proceed accordingly.

Upvotes: 0

Views: 254

Answers (2)

Adam Merckx
Adam Merckx

Reputation: 1214

Perhaps this helps:

import datetime
import os

date = datetime.datetime.today().strftime('%Y-%m-%d')
month = datetime.datetime.today().month
year = datetime.datetime.today().year

mypath = os.path.join("/content", str(year), str(month), str(date))

if not os.path.exists(mypath):
    print("No folder for the current date found!")
else:
    os.chdir(mypath)

Upvotes: 1

Almog-at-Nailo
Almog-at-Nailo

Reputation: 1182

this might be what you're looking for:

from datetime import datetime
import os

today = datetime.today()
date = today.date()
month = today.strftime("%B")
year = today.year

path = os.path.join("/content", str(year), str(month), str(date))
print(path)

Upvotes: 1

Related Questions