【问题标题】:h5py unable to read fast5 fileh5py 无法读取 fast5 文件
【发布时间】:2016-01-23 10:16:14
【问题描述】:

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

【问题讨论】:

    标签: python hdf5 h5py


    【解决方案1】:

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

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

    【讨论】:

    • 哈哈,在我读到你的答案之前我就想通了。不过,你的方法比我的方法干净得多,谢谢!
    猜你喜欢
    • 2016-02-11
    • 2021-02-23
    • 1970-01-01
    • 2013-10-19
    • 2018-08-29
    • 1970-01-01
    • 2015-04-09
    • 2015-10-13
    相关资源
    最近更新 更多