读取Excel文件中的文件名并按照读取的文件名将指定路径下的文件移动到指定的新路径下
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/10/27 10:06:27
# @Author : kevin
# @Site :
# @File : cop_file.py
# @Software: PyCharm
# encoding=utf-8
import shutil
import openpyxl, os
workbook = openpyxl.load_workbook(r'C:\Users\点雨洛山岚\Desktop\matlab结果\607份结果\607份数据筛选前.xlsx') # 需要读取的excel表格(数据名称存在这里)
CopPath_1 = r'D:\python 拷贝测试\新建文件夹 (2)' # 需要移动的文件路径
PastePath_1 = r'D:\python 拷贝测试\脚本拷贝路径' # 移动的数据需要放在那个文件夹下
sheet = workbook['Sheet'] # Excel必须将页面名称改为Sheet,且格式为.xlsx
i, m, a, b = sheet.max_row, 2, 0, 0
list = os.listdir(CopPath_1)
# print(list, len(list))
while i >= 0:
File_name = str(sheet['A' + str(m)].value)[0:]
m += 1
i -= 1
# print(str(" "+list[a]), '\n', File_name, '\n', type(list[a]), '\n', type(File_name))
while True:
if a < len(list):
if str(list[a]) == str(File_name):
CopName = r"%s" % CopPath_1 + '\\' + File_name
PasteName = r"%s" % PastePath_1 + '\\' + File_name
print(CopName, PasteName)
shutil.move(CopName, PasteName)
b += 1
break
else:
a += 1
else:
break
print('===================================================================================================', '\n',
'需要拷贝的标定文件路径中一共含有%s个数据' % a, '本次拷贝了%s个文件' % b, '\n',
'===================================================================================================')
效果图: