Search code examples
pythonfor-loopnestedglob

nested for-loop with glob.iglob function won't work


I try to find pairs of files in two different directories. One File ends with .anno and the other with .png. I try to find them and count how much maches i have. So i tried this:

for AnnoFile in glob.iglob(f'annotations/**/*.anno', recursive=True):
    AnnoFile_name = Path(AnnoFile).stem
    print("1: " + AnnoFile_name)
    for CatFile in glob.iglob(f'categories/**/.png', recursive=True):
        CatFile_name = Path(CatFile).stem
        print("2: " + CatFile_name)
        CatFile_name = CatFile_name.replace("_visu", "")
        if AnnoFile == CatFile_name:
            print("Match found!")
            Counter = Counter + 1
print(Counter)

I tried to first find the ".anno"-File and then search for the matching .png file. The name of the .png-File have a suffix, which is why i replace "_visu" with "". All i see in the console is the print of the first for-loop.


Solution

  • I have found the error. I forgot the '*' before .png. So it must be:

    for AnnoFile in glob.iglob(f'annotations/**/*.anno', recursive=True):
    AnnoFile_name = Path(AnnoFile).stem
    print("1: " + AnnoFile_name)
    for CatFile in glob.iglob(f'categories/**/*.png', recursive=True):
        CatFile_name = Path(CatFile).stem
        print("2: " + CatFile_name)
        CatFile_name = CatFile_name.replace("_visu", "")
        if AnnoFile == CatFile_name:
            print("Match found!")
            Counter = Counter + 1
    print(Counter)