python 文件夹文件批量复制

请问如何用python把多个文件夹中某一固定文件(名称或位置)复制到另一新建文件夹中?

具体描述:
在一个文件夹中有很多个文件夹。每个文件夹里有五张图片,其中每个文件夹中的最后一张图片名字末尾都是_0003,我只想要把所有文件夹中最后这张末尾命名为”_0003“的图片复制到一个新的文件夹中,请问应如何利用python完成这一任务呢?
谢谢大家!

可以这样

import os
import shutil

# 遍历每个子目录


def move_file(root_directory):
    for subdir, dirs, files in os.walk(root_directory):
        # 遍历当前目录下的所有文件
        for file in files:
            # 如果文件名以 '0003.jpg' 结尾,则移动该文件到目标目录
            if file.endswith('_0003'):
                source_file = os.path.join(subdir, file)
                target_file = os.path.join(target_directory, file)
                shutil.move(source_file, target_file)
        for dir in dirs:
            path = os.path.join(root_directory, dir)
            move_file(path)


if __name__ == '__main__':
    # 指定需要查找文件的目录
    root_directory = '/path/to/root/directory'

    # 指定需要移动到的目标目录
    target_directory = '/path/to/target/directory'


import shutil
import os

# 源文件夹路径
src_folder = '/path/to/source/folder'

# 目标文件夹路径
dest_folder = '/path/to/destination/folder'

# 获取源文件夹中的所有文件
files = os.listdir(src_folder)

# 循环遍历并复制所有文件到目标文件夹中
for f in files:
    src_file = os.path.join(src_folder, f)
    dest_file = os.path.join(dest_folder, f)
    shutil.copy(src_file, dest_file)
不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^