-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileFinder.py
82 lines (74 loc) · 2.98 KB
/
FileFinder.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from os.path import splitext
import os
import AnalysableAudioFile
class FileFinder:
audiofiles = []
sizeOfAudiofiles = 0
maxduration = 2
def __init__(self, directories):
errorsFound = False
print("FileFinder - searching for files")
# all userinputted dirs
for directory in directories:
# get all files in input dir
if os.path.exists(directory):
print("\tcurrent dir:", directory)
else:
print("ERROR: dir", directory, " not found!")
num_files = 0
max_files = 300
for root, dir, files in os.walk(directory):
# check every file and add to list
for file in files:
if num_files < max_files:
_, ext = splitext(file)
if ext == ".wav":
af = None
try:
af = AnalysableAudioFile.AnalysableAudioFile(root+"/"+file)
if(af.duration < self.maxduration):
self.audiofiles.append(af)
self.sizeOfAudiofiles += af._size
num_files += 1
except Exception as e:
errorsFound = True
print("\t\t", e)
else:
num_files += 1
elif max_files == num_files:
num_files += 1
print("\t\tfile limit of folder exceeded, limit is", max_files, "files")
break;
if errorsFound:
print("\t\t ERROR - some errors occured while reading the sound files")
print("FileFinder - files found:", len(self.audiofiles))
print("deleted duplicates:", self.deleteDuplicates())
print("FileFinder - files read:", len(self.audiofiles))
# used this function at the end to delete duplicates
def isPathAdded(self, path):
isAdded = False
a = os.path.abspath(path)
for p in self.audiofiles:
b = p.abspath
if a is b:
isAdded = True
# print("true")
return isAdded
def deleteDuplicates(self, numdeletedfiles=0):
isDeleted = False
markedIndices = []
for i in range(len(self.audiofiles)):
af = self.audiofiles[i]
found = False
for j in range(i + 1, len(self.audiofiles)):
if not found:
af2 = self.audiofiles[j]
if af.abspath == af2.abspath:
markedIndices.append(j)
found = True
markedIndices.sort()
markedIndices.reverse()
# print(markedIndices)
for i in markedIndices:
self.audiofiles.remove(self.audiofiles[i])
return len(markedIndices)