Put the folder 2010,2011,label Documents in , Copied to the res/train1 … In the document ,train1 Is a new folder , And rename
import shutil
import os
def read_file_path(path):
# Read file name
return [i for i in os.listdir(path)]
def mkdir(path):
# Create folder
import os
path = path.strip()
path = path.rstrip("\\")
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
# print (path+' Create success ')
return True
else:
# print(path+' directory already exists ')
return False
def copy_file_rename(old_path, new_path):
# Copy files to the specified address
try:
shutil.copy(old_path, new_path)
except Exception as e:
print(e)
def res(path_list):
label = 0
for i in path_list:
file_list = read_file_path(i)
for j in file_list:
old_path = f"{i}/{j}"
mkdir(r'res/train{}'.format(j.split(".")[0]))
if label == 0:
new_path = r'res/train{}'.format(j.split(".")[0]) + "/t0.tif"
elif label == 1:
new_path = r'res/train{}'.format(j.split(".")[0]) + "/t1.tif"
else:
new_path = r'res/train{}'.format(j.split(".")[0]) + "/label.png"
copy_file_rename(old_path, new_path)
label += 1
# Don't put it in the wrong order , Otherwise, something will go wrong
res(["2010", "2011", "label"])
hello , Hello everyone . Today