如何在python中挑选与特定文件夹相关的文件?

问题描述 投票:0回答:1

我有一个业务规定的特定文件夹结构,即 "content2020May13-05-2020",目前我消耗的是这个位置的所有文件,但我希望根据每天的批处理(如文件路径中提到的日期)来挑选消耗文件。

但我更希望的是根据每天的批处理(如文件路径中提到的日期)来选择消耗文件。

为了使它简单,让我们说,在文件路径中,如果今天的日期,五月的月份和2020年存在,那么它应该使用 "内容2020May13-05-2020 "处理文件。

否则,它应该以同样的方式检查年、月和日期,并进行相应的处理。

python filepath
1个回答
1
投票

这可能是你要找的。

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)

1
投票

也许这对你有帮助

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)
© www.soinside.com 2019 - 2024. All rights reserved.