python批量文件重命名

# -*- coding: utf-8 -*-

import os, sys

# Python根据条件修改目录里的文件名:将不想要的删去或者替换掉

# 设定文件路径

# path = 'E:\\Python\\changeFileName\\Miniteco\\'
path = 'D:\\Documents\\MATLAB\\978-7-302-46737-3MATLAB\\chapter12\\'


def rename(path):
# 对目录下的文件进行遍历

for file in os.listdir(path):

# 判断是否是文件(查找以QL开头以.rmvb结尾的文件)
#if (file.startswith("QL") and file.endswith(".rmvb")):

if (file.startswith("ex") and file.endswith(".m")):
print(file)
# 设置新文件名

# newName=file.replace("QL","")#这一句的效果是直接删除QL
newName = file.replace("-", "") # 这一句的效果是将-替换为空
# 重命名

os.rename(os.path.join(path, file), os.path.join(path, newName))


rename(path)
# 结束

print("End")
posted @ 2022-02-27 12:56  洪豆豆的记录  阅读(128)  评论(0编辑  收藏  举报