有些时候我们需要按照excel中的名单查找出文件夹中的文件(word,pdf,txt,png,jpg)都可以,
如excel名单为:(我们需要筛选出的文件是x1,x2)
原始文件夹为:(原始文件夹包含x1,x3)
筛选出的文件夹为:(筛选出来的只包含x1)
import os import numpy as np import pandas as pd import shutil file_path=r'C:\Users\17360\Desktop\test' #文件路径 filename_path=r'C:\Users\17360\Desktop\test1.xlsx' #文件列表 filelist=os.listdir(file_path) #获取文件夹中的文件名称 file_name=pd.read_excel(filename_path) #读取所需文件列表 for file in filelist: print(file) file_name file_name.shape[0] file_name['count']=0 #定义新的一列count,用于计数 for file in filelist: m=file_name.shape[0] #表格的行数 olddir=os.path.join(file_path,file) #每一个文件路径 for i in range(m): if str(file_name['name'][i]) in file: #寻找对应的文件名 F=r"C:\Users\17360\Desktop\myfiles_filter" #新文件夹名称(先建好) newdir=os.path.join(F,file) shutil.copy(olddir,newdir) #复制到新文件夹中 file_name['count'][i]=file_name['count'][i]+1 #计数 print(file) #打印出文件名,为了看它是不是在运行 else: continue file_name.to_excel(r'C:\Users\17360\Desktop\file_name_count.xlsx') #保存新的文件列表,可以统计每个文件出现的次数
有问题欢迎留言哦~~