如何选择 python 中特定文件夹的文件?

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

我有一个公司规定的特定文件夹结构,即“/content/2020/May/13-05-2020”。 目前我正在使用此位置的所有文件。

但我更希望的是 pick/consume 基于每日批处理的文件(如文件路径中提到的日期)。

为简单起见,假设在文件路径中,如果是今天、5 月和 2020 年,那么它应该使用“/content/2020/May/13-05-2020”处理文件。

否则它应该以相同的方式检查年、月和日并相应地进行。

这可能是您要找的:

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)

也许这有帮助:

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)