|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import os
import 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[0] # 表格的行数
olddir = os.path.join (file_path,file) # 每一个文件路径
for i in range(m):
if str (file_name['name'][i]) in file: # 寻找对应的文件名
F = "d:/BBB" # 新文件夹名称(先建好)
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 ('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'][i]) 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'
输出结束,返回值是[1].
用的是3.7版本,感谢 |
|