重新循环直到找到所有匹配项,逻辑?

Re-loop until all matches are found, logic?

我想不通这其中的逻辑。我正在尝试将匹配列表 'matches' 与文件夹中的文件进行比较。如果 'folders' 中的文件等于 'matches' 中的名称,则执行某些操作,但显然它不会 'try' 每个匹配到每个文件。我在想我需要使用 while 循环,但我不知道如何应用它。

import os
import glob
import os.path

folders = glob.glob('C:\Corrections\*.*')
matches = open('filename.txt', 'r')

for each in folders:
    splitname_one = each.split('\', 3)   #Separate the filename from the path
    filename = splitname_one[3]           #Get Filename only
    basefile = filename.split('.', 1)     #Separate filename and file extension
    compare0 = basefile[0]                #assign base file name to compare0
    #print (basefile[0])
    for line in matches:
        match = line.split('.', 1)        #Separe base filename from file extension
        #print (match[1])
        compare1 = match[0]               #assign base file name to compare1
        if compare1==compare0:
            #os.rename(filename, 'C:\holder\' + filename)
            print ('We Have a match!')
        else:
            print ('no match :( ')

FWIW 这是我最终可能会做这样的事情的方式:

import glob
from os.path import basename, splitext

def file_base(filename):
    return splitext(basename(filename))[0]

folders = set(file_base(f) for f in glob.glob('C:\Corrections\*.*'))

with open('filename.txt') as fobj:
    matches = set(file_base(f) for f in fobj.readlines())

print(folders.intersection(matches))