问题描述提取多个嵌套文件夹下面同名文件
目录
在这个文件夹下有三个文件夹
每一个文件夹下又有多个文件夹,要找的图片在这个文件夹里
代码 a
import shutil
from os.path import join
from os import listdir
# 打开文件
path = "E:\图片\Images\mao"
dirs = listdir(path)
new_dirs = "E:\图片\OCRS"
count = 0
for files in dirs:
if files == ".DS_Store":
continue
file_path = join(path, files)
for files_second in listdir(file_path):
if files_second == ".DS_Store":
continue
# file_path_second = join(path + files, files_second)
file_path_second = join(path, files)
file_path_second = join(file_path_second, files_second)
for file in listdir(file_path_second):
if file.startswith("ocr"):
count += 1
new_name = str(count) + ".jpg"
shutil.copy(file_path_second + '/' + file, new_dirs + new_name)
# 如何放到指定的文件夹呢??
如果所有图在一个文件夹下面则
import shutil
from os.path import join
from os import listdir
path = ""E:\图片\Images\mao""
dirs = listdir(path)
new_dirs = "E:\图片\OCRS"
for files in dirs:
if files == ".DS_Store":
continue
file_path = join(path, files)
for file in listdir(file_path):
if file.startswith("1v"):
new_name = files + ".jpg"
shutil.copy(file_path + '/' + file, new_dirs + new_name)
以上代码问题
1 路径前加r,避免特殊字符转义
2 新图片不能放到指定的文件夹
修改如下
代码A
import shutil
from os.path import join
from os import listdir
# 打开文件
path = r"E:\SourceOCR"
dirs = listdir(path)
new_dirs = r"E:\OCR"
count = 0
for files in dirs:
if files == ".DS_Store":
continue
file_path = join(path, files)
for files_second in listdir(file_path):
if files_second == ".DS_Store":
continue
# file_path_second = join(path + files, files_second)
file_path_second = join(path, files)
files_path = join(file_path_second, files_second)
for file in listdir(files_path):
if file.startswith("ocr"):
count += 1
new_name = str(count) + ".jpg"
det_dir = join(new_dirs, new_name)
source_file_dir = files_path + '/' + file
shutil.copy(source_file_dir, det_dir)
# shutil.copyfile(source_file_dir, det_dir)
代码B
import os
import shutil
# 将文件夹中所有的文件名存入一个list
from os.path import join
def find_copy_files(files):
for file in files:
if file == "ocr.jpg":
global count
count += 1
source = secDir + '\\' + str(file)
newFileName = "ocr" + str(count) + ".jpg"
det = join(determination, newFileName)
shutil.copyfile(source, det)
#目标文件夹,此处为相对路径,也可以改为绝对路径
determination = r'E:\OCR'
if not os.path.exists(determination):
os.makedirs(determination)
#源文件夹路径
path = r'E:\SourceOCR'
folders= os.listdir(path)
count = 0
for folder in folders:
dir = path + '\\' + str(folder)
folder_second = os.listdir(dir)
for secfolder in folder_second:
secDir = dir + '\\' + str(secfolder)
files = os.listdir(secDir)
find_copy_files(files)
# for file in files:
# if file == "ocr.jpg":
# count += 1
# source = secDir + '\\' + str(file)
# # deter = determination + str(file)
# newFileName = "ocr" + str(count) +".jpg"
# det = join(determination, newFileName)
# shutil.copyfile(source, det)
假如文件目录为a/b,c,d
图片在bcd文件夹中可以实现
但如果b文件夹里面有b1, b2, b3文件夹则其不能提取
对其修改后可以直接提取所有文件并重新命名,不用考虑文件夹里嵌套了多少
代码C
import os
import shutil
from os.path import join
file_dir = r'E:\SourceOCR'
determination = r'E:\OCR'
count = 0
for roots, dirs, files in os.walk(file_dir): # 注意这里一定要加 roots,否则报错
for dir_name in files:
# 返回所有路径
dir = os.path.join(roots, dir_name)
# 分割路径,返回路径名和文件扩展名的元组
# https://www.runoob.com/python/python-os-path.html
dir_fileName = os.path.splitext(dir)
# print(dir_fileName[0])
# print(dir_fileName)
# 找到需要的文件名
aim = dir_fileName[0].split("\\")[-1]
# print(dir)
if aim == "ocr":
count += 1
NewFileName = str(count) + ".jpg"
det = join(determination, str(count) + ".jpg")
# det = join(determination, NewFileName)
shutil.copyfile(dir, det)
还可以提升用try catch考虑如果文件夹为空或不存在要找的文件名时会出错,以后有空再修改。