shutil.copy2 只复制一个文件并停止,而不是从循环中复制整个文件
shutil.copy2 is copying only one file and stops, instead copying the whole files from loop
我有两个具有 1 个功能的代码(从 src
文件夹中复制文件,这些文件存在于单元格 product_number
中)
问题是代码只复制了 1 个文件,程序没有完成就停止了复制。
我该如何解决?
def copy_images(excel_path):
src = r'D:\src_img'
destination_folder = r'D:\dest_img'
copy_file = openpyxl.load_workbook(excel_path)
data_list = copy_file.active
for path, subdirs, files in os.walk(src):
for name in files:
filename = os.path.join(path, name)
for row in range(2, data_list.max_row + 1):
product_number = str(data_list.cell(row, 1).value)
if product_number in filename:
full_dest_path = os.path.join(destination_folder, filename.lstrip(src).lstrip('/'))
dirname = os.path.dirname(full_dest_path)
if not os.path.exists(dirname):
os.makedirs(dirname)
shutil.copy2(filename, full_dest_path)
def update_images(excel_path):
src = r'D:\src_img'
dest = r'D:\dest_img'
copy_file = openpyxl.load_workbook(excel_path)
data_list = copy_file.active
for path, subdirs, files in os.walk(src):
for name in files:
filename = os.path.join(path, name)
for row in range(2, data_list.max_row + 1):
product_number = str(data_list.cell(row, 1).value)
if product_number in filename:
if not os.path.exists(dest):
os.makedirs(dest)
shutil.copy2(filename, os.path.join(dest, name))
将 shutil.copy2()
行缩进到与 if
语句相同的级别,因此无论目录是否存在,它们都会被执行。
我有两个具有 1 个功能的代码(从 src
文件夹中复制文件,这些文件存在于单元格 product_number
中)
问题是代码只复制了 1 个文件,程序没有完成就停止了复制。
我该如何解决?
def copy_images(excel_path):
src = r'D:\src_img'
destination_folder = r'D:\dest_img'
copy_file = openpyxl.load_workbook(excel_path)
data_list = copy_file.active
for path, subdirs, files in os.walk(src):
for name in files:
filename = os.path.join(path, name)
for row in range(2, data_list.max_row + 1):
product_number = str(data_list.cell(row, 1).value)
if product_number in filename:
full_dest_path = os.path.join(destination_folder, filename.lstrip(src).lstrip('/'))
dirname = os.path.dirname(full_dest_path)
if not os.path.exists(dirname):
os.makedirs(dirname)
shutil.copy2(filename, full_dest_path)
def update_images(excel_path):
src = r'D:\src_img'
dest = r'D:\dest_img'
copy_file = openpyxl.load_workbook(excel_path)
data_list = copy_file.active
for path, subdirs, files in os.walk(src):
for name in files:
filename = os.path.join(path, name)
for row in range(2, data_list.max_row + 1):
product_number = str(data_list.cell(row, 1).value)
if product_number in filename:
if not os.path.exists(dest):
os.makedirs(dest)
shutil.copy2(filename, os.path.join(dest, name))
将 shutil.copy2()
行缩进到与 if
语句相同的级别,因此无论目录是否存在,它们都会被执行。