0
$\begingroup$

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!

$\endgroup$

2 Answers 2

1
$\begingroup$

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)
$\endgroup$
1
  • $\begingroup$ Thanks so much, this worked! $\endgroup$
    – YJay
    Feb 3, 2021 at 15:03
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.