请查看此代码是否对您有帮助。我包括了一个比较两种不同方法的时间的计时器。
import os
from timeit import default_timer as timer
features_to_delete = ['a','b','c']
start = timer()
for x in features_to_delete:
name_checker = str(x) + '.jpg'
print 'this is name checker {}'.format(name_checker)
folder = '.'
for root, dir2, files in os.walk(folder):
print 'This is the root directory at the moment:{} The following are files inside of it'.format(root)
for b in files:
if b.endswith('.jpg'):
local_folder = os.path.join(folder, root)
print 'Here is name of file {}'.format(b)
print 'Here is name of name checker {}'.format(name_checker)
counter = 0
if b == name_checker:
counter += 1
print '{} needs to be deleted..'.format(b)
os.remove(os.path.join(local_folder, b))
print 'Removed {} \n'.format(os.path.join(local_folder, b))
else:
print 'This file can stay {} \n'.format(b)
else:
pass
end = timer()
print(end - start)
start = timer()
features_to_delete = ['d','e','f']
matches = []
folder = '.'
for x in features_to_delete:
x = str(x) + '.jpg'
features_to_delete = [e + '.jpg' for e in features_to_delete]
print 'features' + str(features_to_delete)
for root, dirnames, filenames in os.walk(folder):
for filename in set(filenames).intersection(features_to_delete):#fnmatch.filter(filenames, features_to_delete)# fnmatch.filter(filenames, features_to_delete):
local_folder = os.path.join(folder, root)
os.remove(os.path.join(local_folder, filename))
print 'Removed {} \n'.format(os.path.join(local_folder, filename))
end = timer()
print(end - start)
测试
$ touch foo/bar/d.jpg
$ touch foo/bar/b.jpg
$ python deletefiles.py
this is name checker a.jpg
This is the root directory at the moment:. The following are files inside of it
This is the root directory at the moment:./.idea The following are files inside of it
This is the root directory at the moment:./foo The following are files inside of it
This is the root directory at the moment:./foo/bar The following are files inside of it
Here is name of file d.jpg
Here is name of name checker a.jpg
This file can stay d.jpg
Here is name of file b.jpg
Here is name of name checker a.jpg
This file can stay b.jpg
this is name checker b.jpg
This is the root directory at the moment:. The following are files inside of it
This is the root directory at the moment:./.idea The following are files inside of it
This is the root directory at the moment:./foo The following are files inside of it
This is the root directory at the moment:./foo/bar The following are files inside of it
Here is name of file d.jpg
Here is name of name checker b.jpg
This file can stay d.jpg
Here is name of file b.jpg
Here is name of name checker b.jpg
b.jpg needs to be deleted..
Removed ././foo/bar/b.jpg
this is name checker c.jpg
This is the root directory at the moment:. The following are files inside of it
This is the root directory at the moment:./.idea The following are files inside of it
This is the root directory at the moment:./foo The following are files inside of it
This is the root directory at the moment:./foo/bar The following are files inside of it
Here is name of file d.jpg
Here is name of name checker c.jpg
This file can stay d.jpg
0.000916957855225
features['d.jpg', 'e.jpg', 'f.jpg']
Removed ././foo/bar/d.jpg
0.000241994857788