Where do I put "try:[...]except"? Or how do I pass/ignore/exclude files that return an AttributeError 'NoneType' object has no 'xyz'? I have a huge database of midis and it will make it half way through the files, then throw an attribute error. When I try the try except method it does the same thing.
def read_files(file):
notes=[]
notes_to_parse=None
#parse the midi file
midi=converter.parse(file)
#seperate all instruments from the file
instrmt=instrument.partitionByInstrument(midi)
for part in instrmt.parts:
#fetch data only of Piano instrument
if 'Piano' in str(part):
notes_to_parse=part.recurse()
#iterate over all the parts of sub stream elements
#check if element's type is Note or chord
#if it is chord split them into notes
for element in notes_to_parse:
if type(element)==note.Note:
notes.append(str(element.pitch))
elif type(element)==chord.Chord:
notes.append('.'.join(str(n) for n in element.normalOrder))
#return the list of notes
return notes
#retrieve paths recursively from inside the directories/files
file_path=["Scarlatti"]
all_files=glob.glob('/content/drive/MyDrive/midi/midi/Bach/*.mid',recursive=True)
#reading each midi file
notes_array = np.array([read_files(i) for i in tqdm(all_files,position=0,leave=True)])
#unique notes
notess = sum(notes_array,[])
unique_notes = list(set(notess))
print("Unique Notes:",len(unique_notes))