各位老大帮忙看下是那里出错,没找到问题在那。
import osimport numpy as np
import pandas as pd
import shutil
file_path = 'd:/aaa'# 文件路径
filename_path = 'd:/aaa/old.xlsx'# 文件列表
filelist = os.listdir (file_path)# 获取文件夹中的文件名称
file_name = pd.read_excel (filename_path)# 读取所需文件列表
file_name['count'] = 0# 定义新的一列count,用于计数
for file in filelist:
m = file_name.shape# 表格的行数
olddir = os.path.join (file_path,file)# 每一个文件路径
for i in range(m):
if str (file_name['name']) in file:# 寻找对应的文件名
F = "d:/BBB"# 新文件夹名称(先建好)
newdir = os.path.join(F,file)
shutil.copy (olddir,newdir)# 复制到新文件夹中
file_name['count'] = file_name['count']+1# 计数
print(file)# 打印出文件名,其实我是为了看它是不是在运行
else:
continue
file_name.to_excel ('d:/bbb/new.xlsx')# 保存新的文件列表
出错显视:
运行:D:\Program Files\Python37\python.exe 多文件查找.py
Traceback (most recent call last):
File "D:\Program Files\Python37\lib\site-packages\pandas\core\indexes\base.py", line 3361, in get_loc
return self._engine.get_loc(casted_key)
File "pandas\_libs\index.pyx", line 76, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 5198, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 5206, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'name'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "多文件查找.py", line 17, in <module>
if str (file_name['name']) in file:# 寻找对应的文件名
File "D:\Program Files\Python37\lib\site-packages\pandas\core\frame.py", line 3458, in __getitem__
indexer = self.columns.get_loc(key)
File "D:\Program Files\Python37\lib\site-packages\pandas\core\indexes\base.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: 'name'
输出结束,返回值是.
用的是3.7版本,感谢 if str (file_name['name']) in file:# 寻找对应的文件名
这个代码狠迷惑,原始表格长啥样??? wp231957 发表于 2022-3-24 06:51
if str (file_name['name']) in file:# 寻找对应的文件名
这个代码狠迷惑,原始表格长啥样???
权限不够上传不了图片,就是用excel的列上的名称,查找文件。 wp231957 发表于 2022-3-24 06:51
if str (file_name['name']) in file:# 寻找对应的文件名
这个代码狠迷惑,原始表格长啥样???
原贴:www.jb51.net/article/167520.htm
wp231957 发表于 2022-3-24 06:51
if str (file_name['name']) in file:# 寻找对应的文件名
这个代码狠迷惑,原始表格长啥样???
在网上找一下,(python根据多个文件名批量查找文件)就可找到原贴了,鱼C不能放网址。
有大神能帮忙看一下吗,谢谢了 wgij007 发表于 2022-3-29 08:15
有大神能帮忙看一下吗,谢谢了
把excel表传上来吧,要不然还得给你现编表 wp231957 发表于 2022-3-29 08:26
把excel表传上来吧,要不然还得给你现编表
老大,我也想上传呀,可惜权限不够呀
页:
[1]