How to combine multiple files using OS for PSD analysis

  • MNE version: 0.24.0
  • operating system: macOS 14
directory = '/Users/cinthia/UO/Coding/PD_offmeds'
for dirpath, dirnames, filenames in os.walk(directory):
    for filename in filenames:
        if filename.endswith('.fif'):
            with open(os.path.join(dirpath, filename)) as f:
                print(f.read())

Hi everyone, I’m trying to load multiple eeg .fif files to my code so I can further analyze them all without having to code for all of them separately. I am trying out this code but I get this error:

UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xff in position 33: invalid start byte

Does anyone know what the problem is or if there is any other, easier, way to upload all my files for further analysis?

You cannot read FIFF files with a simple text file handle. Instead, you should use mne.io.read_raw_fif().

Also, please make sure to use the latest MNE version (which is currently 1.6.1; you’re still using 0.24, which is over two years old).

1 Like

You could instead do something like:

from pathlib import Path

directory = Path("/Users/cinthia/UO/Coding/PD_offmeds")
raw_paths = directory.rglob("*.fif")

for raw_path in raw_paths:
    raw = mne.io.read_raw(raw_path)
    ...

Best wishes,
Richard

1 Like