code: def detect_format(filename):
      filename = Path(filename)
      if filename.is_dir():
          if list(filename.glob('*.stc')) and list(filename.glob('*.erd')):
              return Ktlx
          elif (filename / 'patient.info').exists():
              return Moberg
          elif (filename / 'info.xml').exists():
              return EgiMff
          elif list(filename.glob('*.openephys')):
              return OpenEphys
          elif list(filename.glob('*.txt')):
              return Text
          else:
              raise UnrecognizedFormat('Unrecognized format for directory ' +
                                       str(filename))
      else:
          if filename.suffix == '.won':
              return Wonambi
          if filename.suffix.lower() == '.trc':
              return Micromed
          if filename.suffix == '.set':
              return EEGLAB
          if filename.suffix == '.edf':
              return Edf
          if filename.suffix == '.abf':
              return Abf
          if filename.suffix == '.vhdr' or filename.suffix == '.eeg':
              return BrainVision
          if filename.suffix == '.dat':
              try:
                  _read_header_length(filename)
              except (AttributeError, ValueError):
                  pass
              else:
                  return BCI2000
          with filename.open('rb') as f:
              file_header = f.read(8)
              if file_header in (b'NEURALCD', b'NEURALSG', b'NEURALEV'):
                  return BlackRock
              elif file_header[:6] == b'MATLAB':
                  return FieldTrip
          if filename.suffix.lower() == '.txt':
              with filename.open('rt') as f:
                  first_line = f.readline()
                  if '.rr' in first_line[-4:]:
                      return LyonRRI
          else:
              raise UnrecognizedFormat('Unrecognized format for file ' +
                                       str(filename))