Q:

h5py unable to read fast5 file

I'm trying to write data from a fast5 file to a txt file.  I'm able to do so by going into the directory where the files are and using this code:
for filename in os.listdir(os.getcwd()):
    if filename.endswith('.fast5'):
        with h5py.File(filename, 'r') as hdf:
            with open(new_txt, 'a') as myfile:
               myfile.write('%s \t' % (filename))

However, I am now trying to access the files through the main directory, by looping through specific subfolders where the files are located and accessing the files that way, by using this code:
for root, dirs, files in os.walk(path):     
    for d in dirs:
        if d.startswith('pass') or d.startswith('fail')
            for rootfolder, blankdirs, fast5files in os.walk(d):                                                                                                                                                                                                          
                for filename in fast5files:
                    if filename.endswith('.fast5'):
                        with h5py.File(filename, 'r') as hdf:                
                            with open(new_txt, 'a') as myfile:                    
                                myfile.write('%s \t' % (filename))

This code gives the error:
IOError: Unable to open file (Unable to open file: name = 'minion2_chip61_re_n90_yt2_2644_1_ch108_file0_strand.fast5', errno = 2, error message = 'no such file or directory', flags = 0, o_flags = 0) 

which confuses me since it is able to get the filename, but somehow not able to read from it, which it could under the original code.  The error occurs at this line:
with h5py.File(filename, 'r') as hdf: 

Why is h5py not able to open/read the file in this way? 

A:

you need to add the directory os.walk is currently traversing to the filename:
....
if filename.endswith('.fast5'):
    hdf5_path = os.path.join(root, filename)
    with h5py.File(hdf5_path, 'r') as hdf: 
        ...

