用python实现将A文件夹内与B文件夹内同名的文件都复制到C文件夹中
import os
import shutil
# 源文件夹路径
folder_A = 'C:/Users/chenkp/Desktop/test_code/all_file'
folder_B = 'C:/Users/chenkp/Desktop/test_code/fenge_file'
# 目标文件夹路径
folder_C = 'C:/Users/chenkp/Desktop/test_code/C'
# 获取文件夹A和B中的文件名列表
files_A = os.listdir(folder_A)
files_B = os.listdir(folder_B)
# 遍历文件夹A中的文件
for file in files_A:
if file in files_B: # 如果文件在文件夹B中也存在
source_file = os.path.join(folder_A, file) # 源文件路径
destination_file = os.path.join(folder_C, file) # 目标文件路径
shutil.copy2(source_file, destination_file) # 复制文件到文件夹C