简体   繁体   English

Os.walk() 循环检查文件是否存在

[英]Os.walk() loop to check if file exist or not

I got stuck at looping through folders with os.walk.我陷入了使用 os.walk 遍历文件夹的困境。 My code looks like this:我的代码如下所示:

import os
from datetime import timedelta

startD = date(2020,7,10)
day= timedelta(days=1)
EndD = date(2020,7,13)

folder = 'somefolder'
while startD <= EndD:
    
    date=(startD.strftime("%Y%m%d"))
    file = date + 'somename'
    file2 = date + 'somene2'
    for dirpath, subdirs, files in os.walk(folder): 
        for f in files:
            if file in f or file2 in f:
                print(os.path.join(dirpath,f))
            else:
               print("no file for", date)
    startD += day

I have a time period of 4 days (from start to end date with 1 day addition) and I want to find if there is any filename (file and file2) existing in my "folder".我有 4 天的时间段(从开始到结束日期加上 1 天),我想查找我的“文件夹”中是否存在任何文件名(文件和文件 2)。 If file exist I want its full path to be printed, but in other case I want to be notified there is no such filename (file and file2) in folder.如果文件存在,我希望打印其完整路径,但在其他情况下,我希望收到通知,文件夹中没有这样的文件名(文件和文件 2)。

I deliberately deleted "file" and "file2" (for 11.7.2020) from folder in order to check if else statement work, but if I run my code it prints "no file for" + date for EACH FILE IN FOLDER that is not exact as defined.我故意从文件夹中删除了“file”和“file2”(对于 11.7.2020)以检查 else 语句是否有效,但是如果我运行我的代码,它会打印“没有文件”+ 文件夹中的每个文件的日期不是完全按照定义。

no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
no file for 20200713
ETC....

I want to receive results like this (loop for date from 20200710 - 20200714):我想收到这样的结果(循环日期从 20200710 - 20200714):

path for 20200710 + 'somename'
path for 20200710 + 'somene2'
no file for 20200711 + 'somename'
no file for 20200711 + 'somene2'
path for 20200712 + 'somename'
path for 20200712 + 'somene2'
path for 20200713 + 'somename'
path for 20200713 + 'somene2'
path for 20200714 + 'somename'
path for 20200714 + 'somene2'

I am aware that loops through all files and its clear that is prints "no file" for each file that doesn't has exact filename.我知道循环遍历所有文件,并且清楚地为每个没有确切文件名的文件打印“无文件”。 I would like to print out only EXACT filenames that are missing, not all of them.我只想打印出丢失的 EXACT 文件名,而不是全部。

Maybe something like this.也许是这样的。 Take the print file not found outside the loop and use a flag found to check if any file is found.取出循环外file not found的打印file not found ,并使用found的标志来检查是否找到任何文件。 If loop is completed and found is still False then print file not found如果循环完成并found仍为Falsefile not found打印file not found

import os
from datetime import timedelta

startD = date(2020,7,10)
day= timedelta(days=1)
EndD = date(2020,7,13)

folder = 'somefolder'
while startD <= EndD:
    
    date=(startD.strftime("%Y%m%d"))
    file = date + 'somename'
    file2 = date + 'somene2'
    for dirpath, subdirs, files in os.walk(folder): 
        for f in files:
            found = False
            if file in f or file2 in f:
                print(os.path.join(dirpath,f))
                found = True
            else:
                pass
        
        if not found:
            print("no file for", date)

    startD += day

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

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