python - h5py无法读取fast5文件

标签 python hdf5 h5py

我正在尝试将数据从 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无法以这种方式打开/读取文件?

最佳答案

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

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

关于python - h5py无法读取fast5文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33309758/

相关文章:

python - 如何将 JWT 存储在 HttpOnly Cookie 中?

python - 如何从PDF中提取格式化的文本内容

python - Python 中包含图像的 H5 文件 : Want to randomly select without replacement

python - Windows 版 PyTables 的 HDF5 ImportError

python - h5py:压缩管道中的复合数据类型和比例偏移

python - 如何在Windows中使用pip?

python - 如何以节省内存的方式重命名 pandas 数据框(无需创建副本)?

python - 如何合并保存元数据的不同 matlab mat 文件以在 python 中使用?

python - 使用 h5py 压缩现有文件

numpy - 如何连接两个 hdf5 格式的 numpy 数组?