Python修改文件名

示例:删掉文件名中的.2019,即替换.2019为空replace('.2019', '')

import os

# 文件路径
path = 'D:\\庆余年'

# 要替换的字段
rpc = '.2019'

# 获取路径内文件
file = os.listdir(path)

for i in range(len(file)):
    # 原文件名
    n1 = path + '\\' + file[i]
    # 新文件名
    n2 = n1.replace(rpc, '')
    # 调用改名函数,完成改名操作
    os.rename(n1, n2)

# 输出结束的提示信息
print('Over'.center(20, '='))

你可能感兴趣的:(python,开发语言,后端)