以下python程序版本为Python3.13.0
1.请写一个python程序,实现以下逻辑:从文件夹获取所有文件名,与Excel中的fileName列进行对比,凡是不在该文件夹下的文件名,从Excel文档中删除后,并将Excel中fileName和fileLength数据保存至新的excel文档
import os
import pandas as pd
def filter_excel_by_folder(folder_path, excel_path, output_path=None, sheet_name=0, column_name='fileName'):
"""
过滤Excel文件,仅保留文件夹中存在的文件名对应的行
参数:
folder_path (str): 要检查的文件夹路径
excel_path (str): 需要过滤的Excel文件路径
output_path (str, 可选): 输出Excel文件路径,默认为None(覆盖原文件)
sheet_name (str/int, 可选): 工作表名称或索引,默认为第一个工作表
column_name (str, 可选): 包含文件名的列名,默认为'fileName'
"""
try:
# 获取文件夹中的所有文件名(包括扩展名)
folder_files = set(os.listdir(folder_path))
print(f"在文件夹中找到 {len(folder_files)} 个文件")
# 读取Excel文件
df = pd.read_excel(excel_path, sheet_name=sheet_name)
original_count = len(df)
print(f"Excel中原有 {original_count} 行数据")
# 检查文件名列是否存在
if column_name not in df.columns:
raise ValueError(f"Excel中找不到列 '{column_name}'")
# 过滤数据:仅保留文件夹中存在的文件名
filtered_df = df[df[column_name].isin(folder_files)]
new_count = len(filtered_df)
removed_count = original_count - new_count
columns_to_save = ['fileName', 'fileLength']
# 保存需要的列
df_result = filtered_df[columns_to_save]
print(f"删除了 {removed_count} 行不存在于文件夹中的数据")
print(f"保留 {new_count} 行数据")
# 保存结果
if output_path is None:
output_path = excel_path # 覆盖原文件
print("将覆盖原始Excel文件")
else:
print(f"将保存到新文件: {output_path}")
# 保存到Excel
df_result.to_excel(output_path, index=False)
print("操作完成!")
return removed_count
except Exception as e:
print(f"发生错误: {str(e)}")
return -1
2.请写一个python程序,实现以下逻辑:从文件夹获取所有文件名,与Excel中的fileName列进行对比,凡是不在该Excel的文件名,将其从文件夹中删除
import os
import pandas as pd
def delete_unmatched_files(excel_path='', folder_path='', sheet_name=2, column_name='path'):
"""
根据Excel中的文件名删除文件夹中不匹配的文件
参数:
excel_path (str): Excel文件路径
folder_path (str): 要清理的文件夹路径
sheet_name (str/int): Excel工作表名或索引,默认为第一个工作表
column_name (str): 包含文件名的列名,默认为'文件名'
"""
try:
# 从Excel读取文件名列表
df = pd.read_excel(excel_path, sheet_name=sheet_name)
excel_files = set(df[column_name].dropna().astype(str).tolist())
print(f"Excel中找到 {len(excel_files)} 个文件名")
# 获取文件夹中的所有文件
folder_files = set(os.listdir(folder_path))
print(f"文件夹中找到 {len(folder_files)} 个文件")
# 找出需要删除的文件(在文件夹中但不在Excel中的文件)
files_to_delete = folder_files - excel_files
if not files_to_delete:
print("没有需要删除的文件")
return
print(f"找到 {len(files_to_delete)} 个需要删除的文件")
# 删除文件
deleted_count = 0
for file in files_to_delete:
file_path = os.path.join(folder_path, file)
try:
if os.path.isfile(file_path):
os.remove(file_path)
print(f"已删除: {file}")
deleted_count += 1
except Exception as e:
print(f"删除 {file} 时出错: {str(e)}")
print(f"删除完成,共删除了 {deleted_count} 个文件")
except Exception as e:
print(f"程序出错: {str(e)}")