我正在尝试将数据从 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/