-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeleteFiles.py
64 lines (50 loc) · 1.47 KB
/
deleteFiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
# author: EI
# version: 220527a
# python script to delete a lot of folders using many procs
# usage: ./deleteFiles.py <folder>
# libs (some of these needs to be installed via pip)
import sys, os, tqdm, shutil, getopt, multiprocessing, time
# check dataset here
def remove_file(fn,c):
res = False
try:
os.remove(fn)
res = True
except:
pass
return res, c
def main(argv):
opts, fname = getopt.getopt(argv,'hi:o:t')
print(f'removing everying in {fname[0]} using {multiprocessing.cpu_count()} threads\n')
st = time.perf_counter()
# source dir
sDir = os.getcwd()+'/'+fname[0]
# get list of files to be deleted
slist = []
for root, dirs, files in os.walk(sDir):
for file in files:
slist.append(os.path.join(root,file))
# create some threads
pool = multiprocessing.Pool(multiprocessing.cpu_count())
# build task list
tasks = []
for i in range(len(slist)):
tasks.append( (slist[i], i ) )
# assign threads in async manner
res = [pool.apply_async(remove_file,t) for t in tasks]
# looking nice counter
for i in tqdm.tqdm(range(len(tasks))):
if res[i].get()[0]:
pass
# close the threads
pool.close()
pool.join()
# finally remove the empty folder
shutil.rmtree(sDir)
# timer
print(f'finished in {time.perf_counter()-st} seconds!')
if __name__ == "__main__":
main(sys.argv[1:])
sys.exit()
# eof