import os
# 定义文件夹路径
folder_path = './2'
# 打开包含新文件名的txt文档
with open('1.txt', 'r') as file:
# 逐行读取文件名
lines = file.readlines()
# 遍历文件夹中的文件
for i, filename in enumerate(os.listdir(folder_path)):
# 获取文件的原始路径
old_filepath = os.path.join(folder_path, filename)
# 新文件名在txt文档的第i行
new_filename = lines[i].strip() + os.path.splitext(filename)[1]
# 组合新的文件路径
new_filepath = os.path.join(folder_path, new_filename)
# 重命名文件
os.rename(old_filepath, new_filepath)
print("文件重命名完成!")
04-05
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交