Python os 批量修改文件名

时间:2021-05-24 09:25:18   收藏:0   阅读:0
# -*- coding: utf-8 -*-
import os
#设定文件路径
path=r‘D:\郭鹏历届真题解析2007-2020‘
i=1
#对目录下的文件进行遍历
for file in os.listdir(path):
#判断是否是文件
file_name=os.path.join(path,file)
if os.path.isfile(os.path.join(path,file))==True:
#设置新文件名

start=file.find("P")

end=file.find(").flv")

new_name=file[start:end]+".flv"
os.rename(os.path.join(path,file),os.path.join(path,new_name))
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!