我的知识记录

Python批量重命名文件(加前缀/序号)-日常办公最常用的 Python 小工具

import os

def batch_rename(folder, prefix='file_', start=1):
    files = sorted([f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))])
    for i, name in enumerate(files, start=start):
        ext = os.path.splitext(name)[1]
        old = os.path.join(folder, name)
        new = os.path.join(folder, f"{prefix}{i:03d}{ext}")
        os.rename(old, new)
    print(f"已重命名 {len(files)} 个文件")

batch_rename('./照片', prefix='IMG_')

Python批量重命名文件(加前缀/序号)-日常办公最常用的 Python 小工具

标签:

更新时间:2026-09-14 13:11:24

上一篇:python自动扫描当前文件夹里所有 .txt 全部切割

下一篇:Python Excel 合并(多个表合成一个)