Python os 批量修改文件名

# -*- 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))

 

posted @ 2021-05-14 16:10  沭水之虾  阅读(206)  评论(0编辑  收藏  举报