0

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))
Tzadkiel
  • 1
  • 1
  • always put full error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information. – furas May 30 '22 at 00:12
  • first find line which makes problem and use `print()` to see what you have in variables. And next you can use `if variable is not None: ... rest of code ...`. OR use it to exist function with empty result `if variable is None: return []` – furas May 30 '22 at 00:13

1 Answers1

0

how about this? i think it should work, though i don't have your data to test with

import warnings
warnings.filterwarnings('ignore')

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)

  if instrmt:
    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))

  else:
    return None

  #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) if i]) #added if i, so that if the file returns nonetype, it will not be included in notes_array(delete it if you don't want it)

#unique notes
notess = sum(notes_array,[]) 
unique_notes = list(set(notess))
print("Unique Notes:",len(unique_notes))
Pwuurple
  • 352
  • 2
  • 11
  • I think this sort of worked, but now I get: ```TranslateWarning: Unable to determine instrument from ; getting generic Instrument warnings.warn(``` – Tzadkiel May 30 '22 at 01:13
  • That’s likely an error with “ instrmt=instrument.partitionByInstrument(midi)”, if I had to say, it’s likely just that there are instruments in the file that music21 doesn’t recognise – Pwuurple May 30 '22 at 01:30
  • I realise now that it could also be because of the files giving a “nonetype” response, I edited the answer so see if this works now, and if it doesn’t, we can work with what you get from it – Pwuurple May 30 '22 at 01:43
  • thank you but we still get AttributeError: 'NoneType' object has no attribute 'parts'. I don't really get why none of thesr methods are working to ignore NoneType. – Tzadkiel May 31 '22 at 15:06
  • Sorry, I just changed `if instrmt.parts:` to `if instrmt:`, it should work now – Pwuurple Jun 01 '22 at 03:02