python批量重命名文件程序_python批量重命名文件脚本

# 如果有帮助,请点个赞,感谢!

import os

import re

print('【注释:暂支持win系统!】')

def FILEpath():

while True:

inputpath = input('>>输入批量重命名文件夹的绝对路径(例如:D:\新建文件夹):')

filepath = inputpath.strip() # 去除前后空格

if os.path.exists(filepath): # 判断输入文件夹路径是否存在

filenumber = len(os.listdir(filepath)) # 计算文件数量

print('【该文件夹下存在文件数' + str(filenumber) + '。】')

return filepath # 存在即返回路径

else:

print('【提示:请正确输入绝对路径。】')

continue

def NAMED(filepath):

while True:

inputname = input('>>输入新文件名:')

if inputname and re.search(r"\W", inputname) == None: # 判断是否输入文件名以及特殊字符存在

break

else:

print('【提示:请正确输入新文件名且不能包含特殊字符。】')

continue

for file in os.listdir(filepath): # 依次处理文件夹下文件

filename = os.path.splitext(file) # 处理文件名以及后缀名

lnumber = ''

while True:

newname = file.replace(file, inputname + str(lnumber) + filename[1]) # 替换新命名

newfile = os.path.join(filepath, newname) # 拼接文件路径

if newfile == os.path.join(filepath, file): # 判断新命名是否重名原文件

break

else:

if os.path.exists(newfile): # 检查是否存在其它重名文件

if lnumber == '':

lnumber = 0

lnumber += 1

continue

else:

lnumber += 1

continue

else:

os.rename(os.path.join(filepath, file), newfile) # 新命名文件

print('原文件名[' + file + ']更改为:' + newname)

break

if __name__ == '__main__':

NAMED(FILEpath())

原文链接:https://blog.csdn.net/AnotherQuiet/article/details/111660677

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值