我的情况是,在一个大的文件夹中,装了很多子文件夹
。每一个子文件夹
中都有相同数量,相同名字的图片,但是各个子文件夹
是训练的不同阶段,模型生成的结果
编写一个程序,将各子文件夹
中的同名图片汇总一下
部分知识点参考上一篇文章:使用python批量重命名+移动照片
参考: Python 获取文件夹路径名称、路径下所有子目录名称,以及所有非目录子文件名称
father_path = "E:/1021起在服务器上跑程序/1102(2)/compare_results"
# 首先,获取所有子文件夹名,它是装不同阶段图片的文件夹名
import os
for root, dirs, files in os.walk(father_path):
print(root) # 输出当前的文件夹路径
print(dirs) # 当前文件夹路径下所有子文件夹名
print(files) # 当前路径下所有非目录子文件
输出结果:
E:/1021起在服务器上跑程序/1102(2)/compare_results
[‘1200’, ‘1440’, …,‘9360’, ‘960’]
[]
E:/1021起在服务器上跑程序/1102(2)/compare_results\1200
[]
[‘1.png’, ‘10.png’, …, ‘19.png’, ‘2.png’, …, ‘9.png’]
E:/1021起在服务器上跑程序/1102(2)/compare_results\1440
[]
[‘1.png’, ‘10.png’, …, ‘19.png’, ‘2.png’, …, ‘9.png’]
基本上可以看清结构,就是father_path
下有子文件夹['1200', '1440', ...,'9360', '960']
,每个子文件夹中都有图片['1.png', '10.png', ...., '19.png', '2.png', ..., '9.png']
。
那么,获取一下:
import os
sub_dirs,file_names = None,None
for root, dirs, files in os.walk(father_path):
if not sub_dirs and dirs: # 第一次输出的是子文件夹名
sub_dirs = dirs
if not file_names and files: # 适合于father_path中没有文件(有目录没事)的情况
file_names = files
break # 只获取一次,子文件夹中的文件名
print(sub_dirs)
print(file_names)
输出结果:
[‘1200’, ‘1440’, ‘1680’, ‘1920’, ‘2160’, ‘240’, ‘2400’, ‘2640’, ‘2880’, ‘3120’, ‘3360’, ‘3600’, ‘3840’, ‘4080’, ‘4320’, ‘4560’, ‘480’, ‘4800’, ‘5040’, ‘5280’, ‘5520’, ‘5760’, ‘6000’, ‘6240’, ‘6480’, ‘6720’, ‘6960’, ‘720’, ‘7200’, ‘7440’, ‘7680’, ‘7920’, ‘8160’, ‘8400’, ‘8640’, ‘8880’, ‘9120’, ‘9360’, ‘960’]
[‘1.png’, ‘10.png’, ‘11.png’, ‘12.png’, ‘13.png’, ‘14.png’, ‘15.png’, ‘16.png’, ‘17.png’, ‘18.png’, ‘19.png’, ‘2.png’, ‘20.png’, ‘21.png’, ‘3.png’, ‘4.png’, ‘5.png’, ‘6.png’, ‘7.png’, ‘8.png’, ‘9.png’]
首先,如果文件夹不存在,要新建一下文件夹
一个函数:
def mkdir(dir):
"这个函数创建不存在的 路径文件夹"
import os
if not os.path.exists(dir):
os.makedirs(dir)
参考:python shutil 文件(夹)的复制、删除、移动、压缩和解压
使用下面的方法:
import shutil
shutil.copyfile(‘f1.log’, ‘f2.log’) #目标文件无需存在
python获得上级目录路径参考:python获取上一级目录_python获取当前路径和上一级路径
import shutil
target_father_path = os.path.join(father_path,"..") + "/按图片名查看图片/" # 上级文件夹中创建
for pic_name in file_names: # 遍历图片名,有多少个图片,就复制多少次
print("当前正在移动",pic_name)
# 准备当前图片的目标文件夹
target_dir_name = os.path.splitext(pic_name)[0]
target_dir = target_father_path + target_dir_name
mkdir(target_dir) # 创建放分类图片的文件夹
# 进行移动
for dir in sub_dirs: # 遍历原有的子文件夹
file_path = os.path.join(father_path,dir,pic_name)
# 注意,这里名字要改一下,不然同名文件会覆盖前面的
new_pic_name = dir + os.path.splitext(pic_name)[1]
target_file_path = os.path.join(target_father_path, target_dir, new_pic_name)
shutil.copyfile(file_path,target_file_path) # 复制
有几个条件需要满足一下,否则就需要改动:
father_path中没有文件(即
只有
子文件夹目录)的情况
如果使用,只需要修改father_path
即可
import os
import shutil
father_path = "E:/1021起在服务器上跑程序/1102(2)/compare_results"
sub_dirs,file_names = None,None # 子文件夹名, 每个子文件夹都有的文件名
for root, dirs, files in os.walk(father_path):
if not sub_dirs and dirs:
sub_dirs = dirs
if not file_names and files:
file_names = files
break
def mkdir(dir):
"这个函数创建不存在的 路径文件夹"
import os
if not os.path.exists(dir):
os.makedirs(dir)
target_father_path = os.path.join(father_path,"..") + "/按图片名查看图片/" # 在上级文件夹中创建分类图片文件夹的父文件夹
for pic_name in file_names: # 遍历图片名,有多少个图片,就复制多少次
print("当前正在移动",pic_name)
# 准备当前图片的目标文件夹
target_dir_name = os.path.splitext(pic_name)[0]
target_dir = target_father_path + target_dir_name
mkdir(target_dir) # 创建放分类图片的文件夹
# 进行移动
for dir in sub_dirs: # 遍历原有的子文件夹
file_path = os.path.join(father_path,dir,pic_name)
# 注意,这里名字要改一下,不然同名文件会覆盖前面的。这里按文件夹名来命名
new_pic_name = dir + os.path.splitext(pic_name)[1]
target_file_path = os.path.join(target_father_path, target_dir, new_pic_name)
shutil.copyfile(file_path,target_file_path) # 复制