1.获取文件的路径
2.os.listdir模块遍历路径里的文件
3.获取文件名
4.if语句判断是否为文件
5.获取拓展名
6.将新的文件名(字符串就行)和文件后缀拼接在一起
7.使用os模块的函数进行重命名
代码:
import os
while True:
path = input('请输入要重名的文件所在路径:')
try:
list = os.listdir(path)
mum = 0
for i in range(0,len(list)):
filepath = os.path.join(path,list[i])
if os.path.isfile(filepath):
filetye = os.path.splitext(filepath)[1]
template = '{:0>3d}'
newfilename = 'file_'+template.format(mum+1)+filetye
newfilepath = os.path.join(path,newfilename)
os.rename(filepath,newfilepath)
mum+=1
print('批量重命名完成,共处理文件'+str(mum)+'个')
except:
print('请输入一个有效的路径……')
重命名前:
重命名后: