h5py 无法读取 fast5 文件

h5py unable to read fast5 file

我正在尝试将数据从 fast5 文件写入 txt 文件。我可以通过进入文件所在的目录并使用以下代码来做到这一点:

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))

但是,我现在正尝试通过主目录访问文件,方法是循环访问文件所在的特定子文件夹并以这种方式访问​​文件,方法是使用以下代码:

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))

此代码给出错误:

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) 

这让我感到困惑,因为它能够获取文件名,但不知何故无法从中读取,它可以在原始代码下读取。错误发生在这一行:

with h5py.File(filename, 'r') as hdf: 

为什么 h5py 不能以这种方式 open/read 文件?

您需要将os.walk当前遍历的目录添加到文件名:

....
if filename.endswith('.fast5'):
    hdf5_path = os.path.join(root, filename)
    with h5py.File(hdf5_path, 'r') as hdf: 
        ...