Save the files in the target folder to different folders ,( example : distinguish jpg Document and png file ). Other types of files are similar . Multiple file types can also be modified .
import os
import shutil
def split_file(src, dst1, dst2):
'''
function: Separate files with different suffixes into different folders
example: distinguish jpg and png Images
src:str(filefolder)
dst:str(filefolder)
'''
# distinguish jpg and json
jpg = []
json = []
for f in os.listdir(src):
if f.endswith('.png'):
jpg.append(f)
elif f.endswith('.jpg'):
json.append(f)
# Create destination folder
if not os.path.isdir(dst1):
os.mkdir(dst1)
if not os.path.isdir(dst2):
os.mkdir(dst2)
# Copy files to destination folder
for j in jpg:
_jpg = os.path.join(src, j)
shutil.copy(_jpg, dst1)
for p in json:
_png = os.path.join(src, p)
shutil.copy(_png, dst2)
if __name__ == '__main__':
base_filename = r'F:\python_Demo\DeepLearning\tools3\12'
src = os.path.join(base_filename, 'images')
dst1 = os.path.join(base_filename, 'png')
dst2 = os.path.join(base_filename, 'jpg')
split_file(src, dst1, dst2)
in the light of CL-CMU-CD Medium CD_train Data sets , Traverse to get two corresponding RGB The images are saved in two folders , Traversal access GT Save images to a folder , And rename them .
A file directory is attached .
import os
import shutil
# View the paths included in the root directory 、 Folder 、 file os.walk()
def getpath(path):
pathlist=[]
FileList=os.listdir(path)
print(FileList)
for root, dirs, files in os.walk(path):
# print(root)
# print(dirs)
# print(files)
for dir in dirs:
#####################################################################
if dir=='RGB': # choice RGB Folder Use split_file(src, dst1, dst2) when , Use this
# if dir=='GT': # choice GT Folder Use getGT(src, dst3) when , Use this
######################################################################
src=os.path.join(root,dir)
# print(dir)
# print(src)
pathlist.append(src)
print(pathlist)
return pathlist
# Distinguish between different numbers beginning with jpg Images , And save Such as :1_00.jpg 2_00.jpg
def split_file(src, dst1, dst2):
jpg1 = [] # Original filename
jpg1_rename = [] # Renamed file name
jpg2 = []
jpg2_rename = []
for f in os.listdir(src):
x=src.split('\\')[-2] # Cut some folder names (000,001,……), Used to rename
if f.endswith('.jpg'):
info = f.split('_')
before=info[0]
last=info[1]
if before == '1':
jpg1.append(f)
f = before + '_' + x + '_' + last
jpg1_rename.append(f)
if before == '2':
jpg2.append(f)
f = before + '_' + x + '_' + last
jpg2_rename.append(f)
# print(jpg1)
# print(jpg2)
# print(jpg1_rename)
# print(jpg2_rename)
# Create destination folder
if not os.path.isdir(dst1):
os.mkdir(dst1)
if not os.path.isdir(dst2):
os.mkdir(dst2)
# Copy files to destination folder , And rename the file in the destination folder
for j1, j2 in zip(jpg1, jpg1_rename):
src_j1 = os.path.join(src, j1)
shutil.copy(src_j1, dst1)
dst1_j1 = os.path.join(dst1, j1)
dst1_rename = os.path.join(dst1, j2)
print(dst1_j1)
print(dst1_rename)
os.rename(dst1_j1, dst1_rename)
for j1, j2 in zip(jpg2, jpg2_rename):
src_j1 = os.path.join(src, j1)
shutil.copy(src_j1, dst2)
dst2_j1 = os.path.join(dst2, j1)
dst2_rename = os.path.join(dst2, j2)
os.rename(dst2_j1, dst2_rename)
# obtain GT Images , And save
def getGT(src,dst3):
gt1 = [] # Original filename
gt1_rename = [] # Renamed file name
for f in os.listdir(src):
x = src.split('\\')[-2] # Cut some folder names (000,001,……), Used to rename
if f.endswith('.png'):
info = f.split('t')
last = info[1]
gt1.append(f)
f = 'gt_' + x + '_' + last
gt1_rename.append(f)
print(gt1)
print(gt1_rename)
# Create destination folder
if not os.path.isdir(dst3):
os.mkdir(dst3)
# Copy files to destination folder , And rename the file in the destination folder
for g1, g2 in zip(gt1, gt1_rename):
src_g1 = os.path.join(src, g1)
shutil.copy(src_g1, dst3)
dst3_g1 = os.path.join(dst3, g1)
dst3_rename = os.path.join(dst3, g2)
print(dst3_g1)
print(dst3_rename)
os.rename(dst3_g1, dst3_rename)
if __name__ == '__main__':
base_filename = 'F:/python_Demo/DeepLearning/tools3'
src_root = os.path.join(base_filename, 'CD_train01')
srcs = getpath(src_root)
for src in srcs:
print(src)
dst1 = os.path.join(base_filename, 'image01')
dst2 = os.path.join(base_filename, 'image02')
dst3 = os.path.join(base_filename, 'label')
# (1): Put... In multiple folders RGB Copy the image in the folder , And classified according to the initial letter (‘1’ or ‘2’),
# Save to two new folders respectively , And rename
split_file(src, dst1, dst2)
# (2): Put... In multiple folders GT Copy the image in the folder , Save to new folder , And rename
# getGT(src, dst3)
Directory structure :
Build file structure :