Filenotfounderror: [errno 2] no such file or directory: 'nsr001.ecg'

I am trying to save contents of physiobank Normal Sinus Rhythm RR Interval Database into a numpy array but I keep getting an error:

Traceback (most recent call last):
File "AverageRRI.py", line 20, in <module>
averageArray = np.fromfile(file,dtype=float)
FileNotFoundError: [Errno 2] No such file or directory: 'nsr001.ecg'


but the file does exist in the directory.

import os
import numpy as np

for root, dirs, files in os.walk('normal-sinus-rhythm-rr-interval-database-1.0.0'):
for file in files:
if file.endswith(".ecg"):
print(file)
averageArray = np.fromfile(file,dtype=float)
print(averageArray)


When I add the pathname like:

averageArray = np.fromfile('normal-sinus-rhythm-rr-interval-database-1.0.0/nsr001.ecg',dtype=float)
print(averageArray)


It works.

Thanks so much!

You just need to add the root directory in front of the filename so that the full filepath is correct. For this you can use os.path.join:

import os
import numpy as np

for root, dirs, files in os.walk('normal-sinus-rhythm-rr-interval-database-1.0.0'):
for file in files:
if file.endswith(".ecg"):
print(os.path.join(root, file))
averageArray = np.fromfile(os.path.join(root, file), dtype=float)

• Thanks so much, this worked!
– YJay
Feb 3 at 15:03

FileNotFoundError: [Errno 2] No such file or directory: --->'nsr001.ecg'

The directory path you wrote doesnt have the file which you are passing, check the path.