python - 具有多个和代码编辑参数的 shutil.copy() 文件
问题描述
我正在尝试将文件从 3 个父路径复制到不同名称的子路径,我正在努力让它执行拼图的最后一部分!我有 2 个列表:
Cust 与 num 相关且相关:
乔 = 010
鲍勃(父母姓名)= 016
标记 = 018
约翰(鲍勃的孩子)= 016
彼得(鲍勃的孩子)= 016
我设法让我的代码检查源文件夹中是否存在文件路径,如果不存在则用 Bob(父)替换 John 或 Peter(子)。它正确返回文件路径。
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)
输出
'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/'
之后我无法做到的是从每个源文件夹中复制一个文件,其中包含文件名和扩展名(.pdf)中的特定字符串到目标中每个客户名称上方的每个目标路径。我希望将文件复制到以下位置。
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/'
我已经尝试了很多变体for
,if
但我无法复制文件。
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))
我希望这是有道理的,并提前感谢您提出任何问题或建议。