AI办公自动化:用Kimi批量在Excel文件名中加入日期

工作任务:在一个文件夹中所有的Excel文件后面加上一个日期

在Kimi中输入提示词:

你是一个Python编程专家,写一个Python脚本,具体步骤如下:

打开文件夹:F:\AI自媒体内容\AI行业数据分析\投融资

读取里面所有的xlsx文件的主文件名;

在主文件名后面加上"20240528",重命名文件;

注意:每一步都要输出相关信息到屏幕上;

运行程序,返回错误信息:

Traceback (most recent call last):

File "d:\Python\Exceladddate.py", line 25, in <module>

new_file = os.path.join(folder_path, new_file)

NameError: name 'new_file' is not defined

发送给kimi,让其修正:

修改后的源代码:

import os

设置文件夹路径

folder_path = r"F:\AI自媒体内容\AI行业数据分析\投融资"

确保文件夹路径存在

if not os.path.exists(folder_path):

print(f"The directory {folder_path} does not exist.")

else:

列出文件夹中所有的文件

files = os.listdir(folder_path)

过滤出所有的xlsx文件

xlsx_files = [file for file in files if file.endswith('.xlsx')]

遍历xlsx文件列表

for file in xlsx_files:

获取不带扩展名的文件名

base_name = os.path.splitext(file)[0]

新文件名

new_name = f"{base_name}20240528.xlsx"

构造完整的文件路径

old_file = os.path.join(folder_path, file)

new_file = os.path.join(folder_path, new_name) # 确保 new_name 已经定义

try:

重命名文件

os.rename(old_file, new_file)

print(f"Renamed '{file}' to '{new_name}'")

except Exception as e:

print(f"Error renaming file '{file}': {e}")

print("File renaming process completed.")

相关推荐
love530love4 小时前
【ComfyUI】解决 ModuleNotFoundError: No module named ‘inference_core_nodes‘ 问题
人工智能·windows·python·comfyui·inference-core
大模型任我行5 小时前
华为:构建特征级LLM编码评测基准
人工智能·语言模型·自然语言处理·论文笔记
Jason_Honey25 小时前
【平安Agent算法岗面试-二面】
人工智能·算法·面试
Godspeed Zhao5 小时前
现代智能汽车中的无线技术106——ETC(0)
网络·人工智能·汽车
恋猫de小郭5 小时前
AGENTS.md 真的对 AI Coding 有用吗?或许在此之前你没用对?
前端·人工智能·ai编程
久邦科技5 小时前
OpenCode 完整入门(安装 + 配置 + 使用 + 模板)
人工智能
zhangshuang-peta5 小时前
模型上下文协议(MCP):演进历程、功能特性与Peta的崛起
人工智能·ai agent·mcp·peta
heimeiyingwang5 小时前
企业供应链 AI 优化:需求预测与智能调度
大数据·数据库·人工智能·机器学习
bst@微胖子6 小时前
PyTorch深度学习框架之基础实战二
人工智能·深度学习