Loops 在多个文件夹中搜索特定文件(如果存在或未使用通配符)
我在网上搜索过,向人们询问我的“简单”问题,但没有得到令人满意的答案 我的问题如下: 我正在比较来自Extange的数据(以excel文件的形式,保存在一些文件夹中)和我系统中的数据(来自数据库的SQL查询)。我正在设计一个工具来比较从-到特定日期的数据。我所有的exchange数据文件名都基于特定的日期格式,有些字符串和excel文件格式会有所不同(有时是.xls、.xlsx、.xlsm) 显然,我需要做的是编写一个循环,从“from”日期到“to”日期搜索所需的文件。让我们从2020年7月7日到2020年7月13日。strong>比方说,2020年7月11日的文件丢失了。请记住,我的文件存储在某个位置,其中包含多个子文件夹(按月份命名等) 例如:Loops 在多个文件夹中搜索特定文件(如果存在或未使用通配符),loops,wildcard,glob,subdirectory,os.walk,Loops,Wildcard,Glob,Subdirectory,Os.walk,我在网上搜索过,向人们询问我的“简单”问题,但没有得到令人满意的答案 我的问题如下: 我正在比较来自Extange的数据(以excel文件的形式,保存在一些文件夹中)和我系统中的数据(来自数据库的SQL查询)。我正在设计一个工具来比较从-到特定日期的数据。我所有的exchange数据文件名都基于特定的日期格式,有些字符串和excel文件格式会有所不同(有时是.xls、.xlsx、.xlsm) 显然,我需要做的是编写一个循环,从“from”日期到“to”日期搜索所需的文件。让我们从2020年7月7
C:\Users\VB\Desktop\VB\python\05
C:\Users\VB\Desktop\VB\python\06
C:\Users\VB\Desktop\VB\python\07
以下是我的文件名的一些示例:
07.07.2020 - BestScore.xls
07.07.2020 - WorstScore.xlsx
08.07.2020 - BestScore.xls
08.07.2020 - WorstScore.xlsx
09.07.2020 - BestScore.xls
09.07.2020 - WorstScore.xls
10.07.2020 - BestScore.xls
10.07.2020 - WorstScore.xlsm
12.07.2020 - BestScore.xls
12.07.2020 - WorstScore.xlsx
我的基本代码如下所示:
import os
from datetime import timedelta
startD = date(2020,7,10)
day= timedelta(days=1)
EndD = date(2020,7,13)
folder = 'C:\Users\VB\Desktop\VB\python'
while startD <= EndD:
date=(startD.strftime("%d.%m.%Y"))
file = date + '-BestScore'
file2 = date + '-Worstscore'
**code IF file or file2 is found ---> do something **
** ELSE IF file or file2 is not found ---> print(file or file2 not found)
found 07.07.2020 - BestScore.xls
found 07.07.2020 - WorstScore.xlsx
found 08.07.2020 - BestScore.xls
found 08.07.2020 - WorstScore.xlsx
found 09.07.2020 - BestScore.xls
found 09.07.2020 - WorstScore.xls
found 10.07.2020 - BestScore.xls
found 10.07.2020 - WorstScore.xlsm
NOT found 11.07.2020 - Bestscore
NOT found 11.07.2020 - Worstscore
found 12.07.2020 - BestScore.xls
found 12.07.2020 - WorstScore.xlsx
我不需要检查每个文件是否正确,我只想收到如下结果:
import os
from datetime import timedelta
startD = date(2020,7,10)
day= timedelta(days=1)
EndD = date(2020,7,13)
folder = 'C:\Users\VB\Desktop\VB\python'
while startD <= EndD:
date=(startD.strftime("%d.%m.%Y"))
file = date + '-BestScore'
file2 = date + '-Worstscore'
**code IF file or file2 is found ---> do something **
** ELSE IF file or file2 is not found ---> print(file or file2 not found)
found 07.07.2020 - BestScore.xls
found 07.07.2020 - WorstScore.xlsx
found 08.07.2020 - BestScore.xls
found 08.07.2020 - WorstScore.xlsx
found 09.07.2020 - BestScore.xls
found 09.07.2020 - WorstScore.xls
found 10.07.2020 - BestScore.xls
found 10.07.2020 - WorstScore.xlsm
NOT found 11.07.2020 - Bestscore
NOT found 11.07.2020 - Worstscore
found 12.07.2020 - BestScore.xls
found 12.07.2020 - WorstScore.xlsx
总之,我需要一个解决方案,用通配符*搜索多个子文件夹,而不是用IF语句检查每个文件
我学习python已经几个月了,我认为这不是一个需要解决的大问题,但是我有点困惑。
解决此问题将完成我的项目,因为其他一切都已开始工作:)
我很高兴能得到任何帮助
谢谢。你的问题很模糊:如果你能提供更多关于文件系统结构的细节,那就太好了 无论如何,我将您的问题解释为:给定一个要搜索的目录和两个日期(开始和结束),您希望在这些日期内搜索两个不同的文件(BestScore和WorstScore)。如果它们存在,请执行某些操作,否则,请打印警告 例如:
C:\Users\VB\Desktop\VB\python\05
C:\Users\VB\Desktop\VB\python\06
C:\Users\VB\Desktop\VB\python\07
- 开始日期:2020年7月7日
- 结束日期:2020年7月8日
- 搜索目录:~/some/dir
- 允许的文件扩展名:xls、xlsm、xlsx
- ~/some/dir//2020年7月7日-BestScore.xls
- ~/some/dir//2020年7月7日-WorstScore.xlsx
- ~/some/dir//2020年7月8日-BestScore.xlsm
- ~/some/dir//2020年7月8日-WorstScore.xlsx
~/some/dir
的某个子目录中。我们对其他日期的任何其他文件不感兴趣,无论它们是否存在
首先,我们需要一些助手函数。我们从您想要对现有文件执行的任何操作开始,这里用打印
表示
def do_something_与(文件路径):
#用文件做点什么。。
打印(“使用“%s”…”%文件路径执行操作)
用于确定目录项是否为文件且文件类型正确的函数
导入操作系统
def是_xls_文件(文件路径):
return(os.path.isfile(file\u path)和(
文件_path.endswith(“.xls”)或
文件_path.endswith(“.xlsm”)或
文件(path.endswith(“.xlsx”))
用于创建我们正在搜索的文件目录的函数
from datetime导入日期,timedelta
要查找的def文件(开始日期、结束日期、文件名):
文件={}
d=开始日期
而d!=结束日期:
对于文件名中的fn:
文件[%s-%s”%(d.strftime(“%d.%m.%Y”),fn)]=无
d+=时间增量(天数=1)
返回文件
然后是实际搜索功能:我们执行一个os.walk()
,迭代所有文件和子目录。如果我们找到要查找的文件,我们会将它们的路径存储在files\u to\u find
字典中
def find_文件(要查找的文件,搜索目录):
对于os.walk(搜索目录)中的目录路径、子目录和文件:
对于文件中的f:
对于要查找的文件中的ftf:
#add.lower()用于区分大小写
如果f.lower()中的ftf.lower()和is_xls_文件(os.path.join(dirpath,f)):
要查找的文件[ftf]=os.path.join(dirpath,f)
返回要查找的文件
我们可以迭代文件以查找
字典,对存在的文件执行所需的任何操作,并为不存在的文件打印警告
startD=日期(2020,7,10)
EndD=日期(2020年7月13日)
filenames=[“bestscore”,“worstscore”]#搜索不区分大小写
search_dir=“/fold1/fold2”
to_find=文件\u to_find(开始、结束、文件名)
查找=查找文件(要查找,请搜索目录)
对于f,在find.items()中的abs_路径:
如果abs_路径为无:
打印(“无法找到'%s'”%f)
其他:
用(abs路径)做某事
下面是一个示例运行的屏幕截图,其中显示了使用上面输入的文件系统的结果。如前所述,在本例中,脚本将为每个日期搜索六个文件(即最佳和最差分数)。因此,它精确地打印了六个事件,每个文件一个事件:它找到了吗
非常感谢您的详细解释,谢谢。我承认我对问题的描述是模糊的,而不是清晰的。 同时,我已经找到了解决问题的方法,请参见以下内容: 在我的IT collague的帮助下,我们找到了使用glob.glob函数的解决方案
import glob
from datetime import timedelta, date
startD = date(2020,7,10)
day= timedelta(days=1)
EndD = date(2020,7,15)
path = '//some folder'
#print(path)
while startD <= EndD:
date=(startD.strftime("%Y%m%d"))
file = date + '_best_score*' # wildcard because of various extension
file2 = date + '_worst_score*' ## wildcard because of various extension
result = glob.glob(f'{path}/**/{file}', recursive=True) # search through all subfolders of "path"
result2 = glob.glob(f'{path}/**/{file2}', recursive=True) # search through all subfolders of "path"
if result or result2:
print("found file", file)
print("found file", file2)
else:
print("missing", file)
print("missing", file2)
startD += day
导入全局
从日期时间导入时间增量,日期
startD=日期(2020年7月10日)
天=时间增量(天=1)
EndD=日期(2020年7月15日)
路径=“//某个文件夹”
#打印(路径)
当开始时(结果)<2:
如果str中的“最佳分数”(结果):
打印(“缺少”,文件2)
其他:
打印(“缺少”,文件)
其他:
打印(“缺少”,文件)
打印(“缺少”,文件2)
startD+=天
你觉得怎么样
为了进一步学习python,我还将考虑您的代码,再次感谢 当你接受了一个答案并将其标记为已解决时,为什么要重新发布你自己的问题()