简体   繁体   English

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

[英]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".我有一个由企业制定的特定文件夹结构,即“/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".为简单起见,让我们在文件路径中说,如果今天的日期、5 月和 2020 年存在,那么它应该使用“/content/2020/May/13-05-2020”处理文件。

Else it should check for the year, month and date in the same way and proceed accordingly.否则,它应该以相同的方式检查年、月和日期并相应地进行。

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)

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM