I'm trying to copy files from 3 parent paths to child paths of different names and i'm struggling to get it to perform the final part of the puzzle! I have 2 lists:
Cust relate to and are relevant per num:
Joe = 010
Bob (parent name) = 016
Mark = 018
John (child of bob) = 016
Peter (Child of Bob) = 016
I've managed to get my code to check if a filepath exists in the source folders and if it doesnt then replace John or Peter (children) with Bob (Parent). It correctly returns the file paths.
def copyFiles(cust_list, proj_num):
cust_list = ['Joe', 'Bob', 'Mark', 'John', 'Peter']
proj_num = ['010', '016', '018']
for cust, num in zip(cust_list, proj_num):
source = f'C:/Users/Desktop/Automation_Testing/Live_{num} ({cust})/01_Docs_Issued/'
dest = f'C:/Users/Desktop/Automation_Testing/new_{cust}/Data/'
if not os.path.exists(source):
new_cust = cust.replace(cust, 'Bob')
source = source.replace(cust, new_cust)
print(source)
output
'C:/Users/Desktop/Automation_Testing/Live_010 (Joe)/01_Docs_Issued/'
'C:/Users/Desktop/Automation_Testing/Live_016 (Bob)/01_Docs_Issued/'
'C:/Users/Desktop/Automation_Testing/Live_018 (Mark)/01_Docs_Issued/'
'C:/Users/Desktop/Automation_Testing/Live_016 (Bob)/01_Docs_Issued/'
'C:/Users/Desktop/Automation_Testing/Live_016 (Bob)/01_Docs_Issued/'
What I cant get it to do afterwards is copy a file from each Source folder which contains a specific string in filename and extension (.pdf) to each of the dest paths above per customer name in the destination. I'm expecting to copy the file to the following locations.
C:/Users/Desktop/Automation_Testing/new_Joe/Data/'
C:/Users/Desktop/Automation_Testing/new_Bob/Data/'
C:/Users/Desktop/Automation_Testing/new_Mark/Data/'
C:/Users/Desktop/Automation_Testing/new_John/Data/'
C:/Users/Desktop/Automation_Testing/new_Peter/Data/'
I've tried many variations of for
, if
and I just cant it to copy the files across.
for file in os.listdir(source):
if 'INVOICE' in file.upper():
file_type = '.pdf'
if file_type == True:
shutil.copy(os.path.join(source, file), dest)
print(os.path.join(dest, file))
I hope this makes sense and thank you in advance for any questions or advice.