随手记录下处理数据集用的代码,将指定文件类型的文件复制到新文件夹中
# 将images文件夹下所有jpg文件复制到iamges_split文件夹中
import os
import shutil
data_path = 'images'
for root, dirs, files in os.walk(data_path):
for file in files:
if file.endswith("jpg"):
old_file_path = os.path.join(root, file)
#print(old_file_path)
new_path = 'images_split'
if not os.path.exists(new_path): # 创建新文件夹
os.makedirs(new_path)
new_file_path = new_path + '/' + file
print(new_file_path)
shutil.copyfile(old_file_path, new_file_path) # 复制文件
print('finished!')
代码段come from python复制文件到指定文件夹_Robin_Pi的博客-CSDN博客_python 复制指定文件


![[随手记]python文件处理 [随手记]python文件处理](http://www.mshxw.com/aiimages/31/529754.png)
