文件提取代码无法运行,请求修改意见及原理

大家好,我刚学习python不久,现需要让python根据excel中的一列ID号,循环读取一个文件夹中的所有子文件夹内的文件名,并提取匹配成功的文件(注意是文件,不是文件名)。我在网上找到了两个代码,运行之后遇到了两个问题,想请教一下各位如何修改,谢谢!

代码1:

import os
import shutil
src_dir_path = r'D:\1. SEC EDGAR年报数据'  # 源文件夹
to_dir_path = r'D:\12 年报提取尝试'  # 存放复制文件的文件夹
key1 = '关键字'  # 源文件夹中的文件包含字符key则复制到to_dir_path文件夹中
filelist = os.listdir(src_dir_path)  # 当前路径下文件内容列表
def printallfiles(dirs, abspath):
    for file in dirs:
        sub_path = os.path.join(abspath, file)  # 获取文件的绝对路径
        if (os.path.isdir(sub_path)):  # 判断是否为文件夹
            temppath = os.listdir(sub_path)
            printallfiles(temppath, sub_path)  # 递归调用函数,目的是遍历所有文件
        else:
            if key1 in file:
                shutil.copy(sub_path, to_dir_path)
printallfiles(filelist, src_dir_path)  # 调用函数

这个代码运行成功了,但是我的excel表格中,那一列有6000多个数值,如果每个都数值都赋值key1-6000,然后下面同样重复6000个else语句,太麻烦了,python肯定有其他更省力的实现方法,之后我又得到了一个代码,这个是读取表格内容,然后根据内容让python去匹配的:

import os
import pandas as pd
import shutil
# 读取样本全部cik代码excel表到ciklist列表中
df = pd.read_excel(r'D:\11. 年报提取尝试\CIK代码.xlsx')
ciklist = list(map(str, df['cik_full'].tolist()))
print(ciklist)
# 读取"E:\xxx"文件夹下所有txt文件
path = r"D:\1. SEC EDGAR年报数据"
for root, dirs, files in os.walk(path):
    for fn in files:
        if fn.endswith(".txt"):
            if fn[25:45] in ciklist:  # 提取cik代码并判断是否在ciklist列表中
                file_path = os.path.join(root, fn)
                print("文件:", file_path)
                # 把file_path放入新文件夹中
                shutil.copy(file_path, r"D:\12 年报提取尝试")

这个代码我运行的时候,到print (ciklist)这一步完成了,后半部分好像没有运行,就是虽然没报错,end with code 0,但是并没有文件被提取到文件夹中。我不太明白为什么。

之后 我擅自改动了一下,想能否让key1=ciklist呢?改完了以后是这样的

import os
import shutil
import pandas as pd
src_dir_path = r'D:\1. SEC EDGAR年报数据'  # 源文件夹
to_dir_path = r'D:\12 年报提取尝试'  # 存放复制文件的文件夹
df = pd.read_excel(r'D:\11. 年报提取尝试\CIK代码.xlsx')
ciklist = list(map(str, df['cik_full'].tolist()))
key1 = ciklist  # 源文件夹中的文件包含字符key则复制到to_dir_path文件夹中
filelist = os.listdir(src_dir_path)  # 当前路径下文件内容列表
def printallfiles(dirs, abspath):
    for file in dirs:
        sub_path = os.path.join(abspath, file)  # 获取文件的绝对路径
        if (os.path.isdir(sub_path)):  # 判断是否为文件夹
            temppath = os.listdir(sub_path)
            printallfiles(temppath, sub_path)  # 递归调用函数,目的是遍历所有文件
        else:
            if key1 in file:
                shutil.copy(sub_path, to_dir_path)
printallfiles(filelist, src_dir_path)  # 调用函数

然后它就报错了,报错一共有两个:

  if key1 in file:
TypeError: 'in <string>' requires string as left operand, not list

以及,"TypeError: cannot concatenate object of type str"

所以想请教一下各位,这两个代码怎么改才能使我满足任务需要呢?谢谢大家!

朋友,我在你最后一张图的代码中,于原本的17行(else部分)做出了修改,原理是:提取list中的str参与判断,而非直接用list去参与判断(list比str类型不同)

import os
import shutil
import pandas as pd
src_dir_path = r'D:\1. SEC EDGAR年报数据'
to_dir_path = r'D:\12 年报提取尝试'
df = pd.read_excel(r'D:\11. 年报提取尝试\CIK代码.xlsx')
ciklist = list(map(str, df['cik_full'].tolist()))
key1 = ciklist
filelist = os.listdir(src_dir_path)


def printallfiles(dirs, abspath):
    for file in dirs:
        sub_path = os.path.join(abspath, file)
        if (os.path.isdir(sub_path)):
            temppath = os.listdir(sub_path)
            printallfiles(temppath, sub_path)
        else:
            for every_file_name in key1:  # 于此处修改,你不能直接拿列表list去判断有没有在file的字符里,而是循环for提取每个文件名去判断
                if every_file_name in file:
                    shutil.copy(sub_path, to_dir_path)


printallfiles(filelist, src_dir_path)