Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何选择与特定文件夹相关的文件?_Python_Filepath - Fatal编程技术网

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

在python中,如何选择与特定文件夹相关的文件?,python,filepath,Python,Filepath,我有一个由业务部门制定的特定文件夹结构,即“/content/2020/May/13-05-2020”。 目前,我正在使用此位置的所有文件 但我更希望的是基于每日批处理过程(如文件路径中提到的日期)选择/使用文件 为了简单起见,让我们假设在文件路径中,如果今天的日期、5月和2020年存在,那么它应该使用“/content/2020/May/13-05-2020”处理文件 否则,它应该以相同的方式检查年份、月份和日期,并相应地进行操作。这可能就是您要查找的内容: from datetime imp

我有一个由业务部门制定的特定文件夹结构,即“/content/2020/May/13-05-2020”。 目前,我正在使用此位置的所有文件

但我更希望的是基于每日批处理过程(如文件路径中提到的日期)选择/使用文件

为了简单起见,让我们假设在文件路径中,如果今天的日期、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)

这可能是您正在寻找的:

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)
也许这有助于:

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)

您是在哪里遇到问题的?您可以分享一点关于您的问题的详细信息吗?您需要提供详细信息,以便人们正确评估您的问题。直接使用日期构建路径,然后调用os.path.exists方法查看它是否存在。如果是,可以调用os.listdir()方法列出该文件夹中的文件;如果os.path.exists方法返回false,请执行您想要的其他操作。您是在哪里遇到问题的?您可以分享有关您的问题的更多详细信息吗?您需要提供一个方法,以便人们正确评估您的问题。直接使用日期构建路径,然后调用os.path.exists方法查看它是否存在。如果是,可以调用os.listdir()方法列出该文件夹中的文件;如果os.path.exists方法返回false,请执行其他需要的操作。这里有几个问题,首先,您没有在
mypath
中保留绝对路径,因为您没有在开始时放置
/
。第二,您使用
os.chdir
访问路径,然后检查它是否存在,应该是相反的。感谢您的反馈:)感谢您的支持。这里有几个问题,首先,您没有在
mypath
中保留绝对路径,因为您没有在开始时放置
//code>。第二,您使用
os.chdir
访问路径,然后检查它是否存在,什么时候应该是相反的。感谢您的反馈:)感谢您的支持。