import os, re
while True:
keyword = input("请输入你要删除的字符串:")
if len(keyword) == 0 or keyword.isspace():
print("字符串不能为空!")
else:
break
suffix = input("需要筛选的文件名后缀(Enter代表所有):")
fileNames = os.listdir() # 获取当前目录下的所有文件
for file in fileNames:
check = os.path.join(os.path.abspath('.'), file)
if os.path.isfile(check):
if len(suffix) == 0 or suffix.isspace():
if keyword in file:
print(file, " -> ", file.replace(keyword, ''))
os.rename(file, file.replace(keyword, ''))
else:
# 用正则表达式匹配后缀名
if re.match('.+?\.' + suffix + '$', file) != None and keyword in file:
print(file, " -> ", file.replace(keyword, ''))
os.rename(file, file.replace(keyword, ''))
删减文件名字的关键字
最新推荐文章于 2023-02-19 00:24:26 发布