删除含有某个字符的文件

在一个数据集中有1.png1_1.png,这是一个pair对,现在不想要带有下划线的图片了,下面程序可以批量删除带有_字符的图片

1
2
3
4
5
6
7
8
9
10
11
import os

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:
if file.find('_') is not -1:
os.remove(os.path.join(path, file))
print(file, "ok")
a = a+1
Error: Not Found