'Trouble copying several image files with the same name into existing folder

I have a directory ("E:images") consisting of several hundred folders. Each folder contains about five separate image files. I would like to take a specific image file from each folder and place them in a separate folder called "test". The image in each folder starts with "state_0". I feel like I should be able to copy each file by that string but I'm not even able to transfer any images by file type at the moment.

This seems like it should be pretty straight forward but I'm not getting any errors and nothing is being copied into my folder when I run the script.

This is what I have:

import os
import shutil
import glob


files = glob.iglob(os.path.join(r'E:\\images', "*.bmp"))
for file in files:
    if os.path.isfile(file):
        shutil.copy2(file, r'E:\\test')


Solution 1:[1]

import os
import glob
import shutil

files = glob.iglob(os.path.join(r'E:\images', '*.bmp'))

for file in files:
    print(file)
    # if file is a bmp and has state_0 in filename
    if file.endswith('bmp') and 'state_0' in file:
        print(file)
        # copy file into test 
        shutil.copy2(file, r'E:\test')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1