本帖最后由 jackz007 于 2022-11-29 13:29 编辑
看看这个是否符合要求?import os
p , d , e , k = r'.\aaa' , [] , {} , 0
for each in os . listdir(p) :
x = os . path . join(p , each)
if os . path . isfile(x) :
base , ext = os . path . splitext(each)
if ext[1 :] . isdigit() :
if not base in e . keys() :
d . append([])
e[base] = k
k += 1
d[e[base]] . append((int(ext[1 :]) , x))
for x in d :
x . sort(reverse=True)
for y in x:
if y[0] != x[0][0] : print(y[1] , '- * remove * -')
else : print(y[1])
如果确认删除的文件没有问题,那就直接运行下面这个代码:import os
p , d , e , k = r'.\aaa' , [] , {} , 0
for each in os . listdir(p) :
x = os . path . join(p , each)
if os . path . isfile(x) :
base , ext = os . path . splitext(each)
if ext[1 :] . isdigit() :
if not base in e . keys() :
d . append([])
e[base] = k
k += 1
d[e[base]] . append((int(ext[1 :]) , x))
for x in d :
x . sort(reverse=True)
for y in x:
if y[0] != x[0][0] : os . remove(y[1])
|