【Python】Python批量修改文件名
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
# path = input('请输入文件路径(结尾加上/):')
path = "D:\\soft\\music\\tzq\\20240815-2351-tzq"
# 获取该目录下所有文件,存入列表中
fileList = os.listdir(path)
new_name = ""
n = 0
for i in fileList:
old_name = path + os.sep + fileList[n] # os.sep添加系统分隔符
new_name = ""
if old_name.find(".mp3") > 0:
new_name = old_name.rstrip(".mp3")
new_name = new_name + "-(tzq专属).mp3"
os.rename(old_name, new_name) # 用os模块中的rename方法对文件改名
print(old_name, '======>', new_name)
n += 1