删除文件中大于某个数字的文件

在一个数据集中有1.png10000.png,现在不想要大于5000.png的图片了,用正则表达式来实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os
import re

path = "C:\\Users\\Administrator\\Desktop\\testB" # 文件路径
a = 1
for file in os.listdir(path):
b = os.path.join(path, file)
if os.path.isfile(os.path.join(path, file))==True:
regex = re.compile(r'\d+')
num = int(max(regex.findall(file)))
if num > 5000:
os.remove(os.path.join(path, file))
print(file, "ok")
a = a+1