my attempt:
import numpy as np
file_name_and_desti_subdir = np.array([['1020','A'],
['1020','A'],
['1106','A'],
['1003','B'],
['1003','B'],
['1004','C'],
['1005','C']]) #.astype('object')
print(file_name_and_desti_subdir, file_name_and_desti_subdir.size, file_name_and_desti_subdir.shape,
file_name_and_desti_subdir.ndim, file_name_and_desti_subdir.dtype)
from shutil import copy,copyfile
from os import walk, path, makedirs
def copyFilez(source, desti, file_name_and_desti_subdir):
nA = 1
nB = 1
nC = 1
for item in file_name_and_desti_subdir:
print('ITEM :', item)
for root, subdir, files in walk(source): #using os.walk to find correct item in source
print('root : ',root)
print('subdir :',subdir)
for file in files:
print(file, item[1])
item_source_path = path.join(root, file) #constructing source path of item
print('item_source_path : ', item_source_path)
if file.split('.')[0] == item[0]: #choice structure
if item[1] == 'A':
print(desti + "/A/"+file)
if not path.exists(desti + "/A"):
makedirs(desti + "/A", exist_ok=True)
if path.isfile('/'+desti + r"/A/"+file) == True:
copy(item_source_path, '/'+desti + "/A/"+file.split('.')[0]+'_'+str(nA)+'.'+file.split('.')[1])
nA += 1
else:
copyfile(item_source_path, desti + "/A/"+file )
elif item[1] == 'B':
if not path.exists(desti + "/B/"):
makedirs(desti + "/B", exist_ok=True)
if not path.isfile(desti + "/B/"+file):
copy(item_source_path, desti + "/B/"+file)
else:
copy(item_source_path, desti + "/B/"+file.split('.')[0]+'_'+str(nB)+'.'+file.split('.')[1])
nB += 1
elif item[1] == 'C':
if not path.exists(desti + "/C"):
makedirs(desti + "/C", exist_ok=True)
if not path.isfile(desti + "/C/"+file):
copy(item_source_path, desti + "/C/"+file)
else:
copy(item_source_path, desti + "/C/"+file.split('.')[0]+'_'+str(nC)+'.'+file.split('.')[1])
nC += 1
copyFilez('SOURCE', 'DEST', file_name_and_desti_subdir)
as suggested by Cristhopher I remade the script numbering each one of the files and using array to calculate the times each file appears in the different sections:
from shutil import copy
from os import walk, path, makedirs
import numpy as np
file_name_and_desti_subdir = np.array([['1020', 'A'],
['1020', 'A'],
['1106', 'A'],
['1003', 'B'],
['1003', 'B'],
['1004', 'C'],
['1005', 'C'],
['1205', 'A'],
['1205', 'A'],
['1205', 'A'],
['1205', 'A'],
['1205', 'B'],
['1205', 'C']]) # .astype('object')
def copyFilez(source, desti, file_name_and_desti_subdir_copy):
file_name_and_desti_subdir_copy = np.zeros((file_name_and_desti_subdir.shape[0]) ,dtype = "object")
for i in range(file_name_and_desti_subdir.shape[0]):
file_name_and_desti_subdir_copy[i] = file_name_and_desti_subdir[i,0]+file_name_and_desti_subdir[i,1]
file_name_and_desti_subdir_copy2 = np.zeros((file_name_and_desti_subdir.shape[0],4) ,dtype = "object")
for i in range(file_name_and_desti_subdir_copy.shape[0]):
file_name_and_desti_subdir_copy2[i,0] = file_name_and_desti_subdir[i,0]
file_name_and_desti_subdir_copy2[i,1] = file_name_and_desti_subdir[i,1]
file_name_and_desti_subdir_copy2[i,2] = file_name_and_desti_subdir_copy[i]
file_name_and_desti_subdir_copy2[i,3] = str(np.count_nonzero(file_name_and_desti_subdir_copy[:i+1] == file_name_and_desti_subdir_copy[i])).zfill(6)
print(file_name_and_desti_subdir_copy2, file_name_and_desti_subdir_copy2.size, file_name_and_desti_subdir_copy2.shape)
for item in file_name_and_desti_subdir_copy2:
print('ITEM :', item)
# using os.walk to find correct item in source
for root, subdir, files in walk(source):
print('root : ', root)
print('subdir :', subdir)
for file in files:
print(file, item[1])
# constructing source path of item
item_source_path = path.join(root, file)
print('item_source_path : ', item_source_path)
if file.split('.')[0] == item[0]: # choice structure
if item[1] == 'A':
print(desti + "/A/"+file)
if not path.exists(desti + "/A"):
makedirs(desti + "/A", exist_ok=True)
copy(item_source_path, desti + "/A/"+file.split('.')[0]+"_"+ item[3] + "." +file.split('.')[1])
elif item[1] == 'B':
if not path.exists(desti + "/B/"):
makedirs(desti + "/B", exist_ok=True)
copy(item_source_path, desti + "/B/"+file.split('.')[0]+"_"+ item[3] + "." +file.split('.')[1])
elif item[1] == 'C':
if not path.exists(desti + "/C"):
makedirs(desti + "/C", exist_ok=True)
copy(item_source_path, desti + "/C/"+file.split('.')[0]+"_"+ item[3] + "." +file.split('.')[1])
copyFilez('SOURCE', 'DESTinazione', file_name_and_desti_subdir)
it goes through creating two new arrays the last one here:
[['1020' 'A' '1020A' '000001']
['1020' 'A' '1020A' '000002']
['1106' 'A' '1106A' '000001']
['1003' 'B' '1003B' '000001']
['1003' 'B' '1003B' '000002']
['1004' 'C' '1004C' '000001']
['1005' 'C' '1005C' '000001']
['1205' 'A' '1205A' '000001']
['1205' 'A' '1205A' '000002']
['1205' 'A' '1205A' '000003']
['1205' 'A' '1205A' '000004']
['1205' 'B' '1205B' '000001']
['1205' 'C' '1205C' '000001']]
and counts with np.count_nonzero
zeros to the numbering are added by str(number)zfill()